前面的话】SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它配置简单,上手快,而且生态成熟,便于应用。但是它对SpringBoot有很强的依赖,需要有一定基础,但是SpringBoot俩小时就可以入门。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。另外这是SpringCloud的版本为Greenwich.SR2,JDK版本为1.8,SpringBoot的版本为2.1.7.RELEASE


  • 新建一个Maven父工程lovincloud,便于版本管理,然后删除src文件夹
  • 添加pom依赖和SpringCloud和SpringBoot的版本
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.7.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <groupId>com.eelve.lovincloud</groupId>
  8. <artifactId>lovincloud</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>pom</packaging>
  11. <name>lovincloud</name>
  12. <url>http://maven.apache.org</url>
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-test</artifactId>
  22. <scope>test</scope>
  23. </dependency>
  24. </dependencies>
  25. <dependencyManagement>
  26. <dependencies>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-dependencies</artifactId>
  30. <version>${spring-cloud.version}</version>
  31. <type>pom</type>
  32. <scope>import</scope>
  33. </dependency>
  34. </dependencies>
  35. </dependencyManagement>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. </plugin>
  42. </plugins>
  43. </build>

在这里,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。

  • 新建一个子工程lovin-eureka-server作为服务的注册中心
  1. <parent>
  2. <artifactId>lovincloud</artifactId>
  3. <groupId>com.eelve.lovincloud</groupId>
  4. <version>1.0-SNAPSHOT</version>
  5. </parent>
  6. <modelVersion>4.0.0</modelVersion>
  7. <artifactId>lovin-eureka-server</artifactId>
  8. <packaging>jar</packaging>
  9. <name>eurekaserver</name>
  10. <version>0.0.1</version>
  11. <description>eureka服务端</description>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>
  22. </dependencies>
  23. <dependencyManagement>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-dependencies</artifactId>
  28. <version>${spring-cloud.version}</version>
  29. <type>pom</type>
  30. <scope>import</scope>
  31. </dependency>
  32. </dependencies>
  33. </dependencyManagement>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-maven-plugin</artifactId>
  39. </plugin>
  40. </plugins>
  41. </build>
  • 然后在启动类上添加@EnableEurekaServer注解:
  1. package com.eelve.lovin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @ClassName LovinEurekaServerApplication
  7. * @Description TDO
  8. * @Author zhao.zhilue
  9. * @Date 2019/8/15 16:20
  10. * @Version 1.0
  11. **/
  12. @EnableEurekaServer
  13. @SpringBootApplication
  14. public class LovinEurekaServerApplication {
  15. public static void main(String[] args) {
  16. SpringApplication.run(LovinEurekaServerApplication.class,args);
  17. }
  18. }
  • eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
  1. spring:
  2. application:
  3. naem: lovineurkaserver # 服务模块名称
  4. server:
  5. port: 8881 # 设置的eureka端口号
  6. eureka:
  7. instance:
  8. hostname: localhost # 设置eureka的主机地址
  9. client:
  10. registerWithEureka: false #表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false
  11. fetchRegistry: false #表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false
  12. serviceUrl:
  13. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #Eureka server地址,查询服务和注册服务都需要依赖这个地址,多个地址可用逗号(英文的)分割
  • 新建一个子工程lovin-eureka-server作为服务的注册中心
  1. <parent>
  2. <artifactId>lovincloud</artifactId>
  3. <groupId>com.eelve.lovincloud</groupId>
  4. <version>1.0-SNAPSHOT</version>
  5. </parent>
  6. <modelVersion>4.0.0</modelVersion>
  7. <artifactId>lovin-eureka-client</artifactId>
  8. <packaging>jar</packaging>
  9. <name>eurekaclient</name>
  10. <version>0.0.1</version>
  11. <description>eureka的一个消费端</description>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. </dependencies>
  22. <build>
  23. <plugins>
  24. <plugin>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-maven-plugin</artifactId>
  27. </plugin>
  28. </plugins>
  29. </build>
  • 然后在启动类上添加@EnableEurekaClient注解:
  1. package com.eelve.lovin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. /**
  6. * @ClassName LovinEurekaClientApplication
  7. * @Description TDO
  8. * @Author zhao.zhilue
  9. * @Date 2019/8/15 16:37
  10. * @Version 1.0
  11. **/
  12. @SpringBootApplication
  13. @EnableEurekaClient
  14. public class LovinEurekaClientApplication {
  15. public static void main(String[] args) {
  16. SpringApplication.run(LovinEurekaClientApplication.class,args);
  17. }
  18. }
  • 然后我们需要连接到服务端,具体配置如下
  1. server:
  2. port: 8801 # 服务端口号
  3. spring:
  4. application:
  5. name: lovineurkaclient # 服务名称
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://localhost:8881/eureka/ # 注册到的eureka服务地址
  • 新建一个Controller写一个测试接口
  1. package com.eelve.lovin.controller;
  2. import com.eelve.lovin.config.ServerConfig;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /**
  7. * @ClassName HelloController
  8. * @Description TDO应用默认访问接口
  9. * @Author zhao.zhilue
  10. * @Date 2019/8/15 16:45
  11. * @Version 1.0
  12. **/
  13. @RestController
  14. public class HelloController {
  15. @Autowired
  16. ServerConfig serverConfig;
  17. @RequestMapping("hello")
  18. public String hello(){
  19. return serverConfig.getUrl()+"###"+ HelloController.class.getName();
  20. }
  21. }

访问localhost:8881查看结果
注册中心
到这里我们可以已经看到已经成功将客户端注册到服务端了,然后我们访问测试接口
192202
可以看到已经访问成功,至此Eureka的搭建已经完成。

在互联网中我们一般都会考虑安全性,尤其是管理服务的注册中心,所以我们可以用spring-boot-starter-security来做安全限制

  • lovin-eureka-server添加spring-boot-starter-security的pom依赖
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
  • 修改配置文件
  1. spring:
  2. application:
  3. naem: lovineurkaserver # 服务模块名称
  4. security:
  5. basic:
  6. enabled: true
  7. user:
  8. name: lovin
  9. password: ${REGISTRY_SERVER_PASSWORD:lovin}
  10. server:
  11. port: 8881 # 设置的eureka端口号
  12. eureka:
  13. instance:
  14. hostname: localhost # 设置eureka的主机地址
  15. metadata-map:
  16. user.name: ${security.user.name}
  17. user.password: ${security.user.password}
  18. client:
  19. registerWithEureka: false #表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false
  20. fetchRegistry: false #表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false
  21. serviceUrl:
  22. defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ #Eureka server地址,查询服务和注册服务都需要依赖这个地址,多个地址可用逗号(英文的)分割
  • 添加security配置
  1. package com.eelve.lovin.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. /**
  7. * @ClassName SecurityConfig
  8. * @Description TDO
  9. * @Author zhao.zhilue
  10. * @Date 2019/8/16 14:13
  11. * @Version 1.0
  12. **/
  13. @EnableWebSecurity
  14. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  15. @Override
  16. protected void configure(HttpSecurity http) throws Exception {
  17. http.csrf().disable();
  18. http.authorizeRequests()
  19. .antMatchers("/css/**").permitAll()
  20. .anyRequest().authenticated()
  21. .and()
  22. .formLogin()
  23. .and()
  24. .httpBasic();
  25. super.configure(http);
  26. }
  27. }
  • lovin-eureka-client添加spring-boot-starter-security的pom依赖
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
  • 修改yaml配置文件
  1. server:
  2. port: 8801 # 服务端口号
  3. spring:
  4. application:
  5. name: lovineurkaclient # 服务名称
  6. security:
  7. basic:
  8. enabled: true
  9. user:
  10. name: lovin
  11. password: ${REGISTRY_SERVER_PASSWORD:lovin}
  12. eureka:
  13. client:
  14. serviceUrl:
  15. defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注册到的eureka服务地址
  16. instance:
  17. leaseRenewalIntervalInSeconds: 10
  18. health-check-url-path: /actuator/health
  19. metadata-map:
  20. user.name: lovin
  21. user.password: lovin
  • 添加security配置
  1. package com.eelve.lovin.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. /**
  6. * @ClassName SecurityConfig
  7. * @Description TDO
  8. * @Author zhao.zhilue
  9. * @Date 2019/8/16 14:13
  10. * @Version 1.0
  11. **/
  12. @Configuration
  13. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http.authorizeRequests().anyRequest().permitAll()
  17. .and().csrf().disable();
  18. }
  19. }
  • 另外为了测试多客服端注册,我们可以修改再给客户端新建一个配置文件,然后开启IDEA的多节点运行,如下图所示勾选Allow parallel run
    192203
  • 然后为了区分是哪个节点的请求我们可以添加获取端口
  1. package com.eelve.lovin.config;
  2. import org.springframework.boot.web.context.WebServerInitializedEvent;
  3. import org.springframework.context.ApplicationListener;
  4. import org.springframework.stereotype.Component;
  5. import java.net.InetAddress;
  6. import java.net.UnknownHostException;
  7. /**
  8. * @ClassName ServerConfig
  9. * @Description TDO
  10. * @Author zhao.zhilue
  11. * @Date 2019/8/18 12:03
  12. * @Version 1.0
  13. **/
  14. @Component
  15. public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
  16. private int serverPort;
  17. public String getUrl() {
  18. InetAddress address = null;
  19. try {
  20. address = InetAddress.getLocalHost();
  21. } catch (UnknownHostException e) {
  22. e.printStackTrace();
  23. }
  24. return "http://"+address.getHostAddress() +":"+this.serverPort;
  25. }
  26. @Override
  27. public void onApplicationEvent(WebServerInitializedEvent event) {
  28. this.serverPort = event.getWebServer().getPort();
  29. }
  30. }
  • 然后我们一次重启服务端和两个客户端,这个时候我们访问http://localhost:8881/
    192205
    可以看到,这里已经让我们输入用户名和密码了,说明spring-boot-starter-security已经配置成功,这时我们输入配置的用户名:lovin和密码:lovin
    192204
    这里我们可以看到已经成功了,那么到这里Eureka的配置已经全部成功了。
  • 最后的最后是本博客的源码,欢迎关注这一套SpringCloud的实践

版权声明:本文为eelve原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/eelve/p/11409301.html