spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2


建议先看第一篇:spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读

接下来我们开始一个新的模块: config 分布式配置中心模块
在项目 zzf-spring-cloud-Finchley 项目下创建模块命名为: config-server
这个是配置中心的服务端。
  • 先是添加依赖:
  1. <dependency>  
  2.     <groupId>org.springframework.cloud</groupId>  
  3.     <artifactId>spring-cloud-config-server</artifactId>  
  4. </dependency>  

  • 创建启动类:
  1. @EnableConfigServer  
  2. @SpringBootApplication  
  3. public class ConfigServerApplication {  
  4.   
  5.     public static void main(String[] args) {  
  6.         SpringApplication.run(ConfigServerApplication.class, args);  
  7.     }  
  8. }  

其中@EnableConfigServer 是启用配置
  • 创建application.yml 配置文件, 添加如下内容, 这里使用properties 文件是因为, yml在显示端点的时候有bug, bug 会在后面贴出来。
  1. spring:  
  2.   cloud:  
  3.     config:  
  4.       server:  
  5.         git:  
  6.           uri: https://gitee.com/zhongzunfa/spring-cloud-config.git  
  7.           search-paths: V1-DEV  
  8.   application:  
  9.       name: config-server  
  10. server:  
  11.     port: 9092 #启动端口  


说明: uri 是git的地址, search-paths 是git 下面的一个目录下图显示, 在实际开发中通过文件夹进行区分。
如果你的操作需要用户名和密码的话添加, 和uri 平级username, password
例如: username: user
password: pass
git 上文件夹里面的内容

spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2

spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2
服务端搭建完成后我, 我们开始创建客户端:
在项目 zzf-spring-cloud-Finchley 项目下创建模块命名为: config-client-consumer
  • 先是添加依赖:
  1. <dependency>  
  2.     <groupId>org.springframework.cloud</groupId>  
  3.     <artifactId>spring-cloud-starter-config</artifactId>  
  4. </dependency>  
  5.   
  6. <dependency>  
  7.     <groupId>org.springframework.cloud</groupId>  
  8.     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
  9. </dependency>  
  10.   
  11. <dependency>  
  12.     <groupId>org.springframework.boot</groupId>  
  13.     <artifactId>spring-boot-starter-web</artifactId>  
  14. </dependency>  

  • 创建ConfigClientConsumerApplication 类
  1. @SpringBootApplication  
  2. @EnableDiscoveryClient  
  3. public class ConfigClientConsumerApplication {  
  4.   
  5.     public static void main(String[] args) {  
  6.         SpringApplication.run(ConfigClientConsumerApplication.class, args);  
  7.     }  
  8. }  

这里添加@EnableDiscoveryClient 这个是为了注册到注册中心, 为后续的zuul 铺垫。
  • 创建application.yml 添加如下配置信息
  1. spring:  
  2.   application:  
  3.     name: config-client-consumer  
  4. server:  
  5.   port: 9093  

  • 创建一个启动加载文件 bootstrap.yml 添加如下信息:
  1. spring:  
  2.     cloud:  
  3.         config:  
  4.             label: master  
  5.             uri: http://localhost:9092  
  6.             name: author,config-info,default  

# 需要注意的是, author 就是git 上配置的文件名称

  • 创建一个配置author 信息加载的类: AuthorConfig
  1. @Component  
  2. @ConfigurationProperties(prefix = "spring.zzf")  
  3. public class AuthorConfig {  
  4.   
  5.     private String name;  
  6.     private Integer age;  
  7.     private Integer sex;  
  8.    
  9.     .....  
  10. }  

这里省列get set 方法。

  • 创建controller 类:
  1. @Controller  
  2. @RequestMapping("configConsumer")  
  3. public class ConfigConsumerController {  
  4.   
  5.     @Autowired  
  6.     private AuthorConfig authorConfig;  
  7.     @RequestMapping("/getAuthorInfo")  
  8.     @ResponseBody  
  9.     public String getAuthorInfo(){  
  10.         return " author信息是丛git上加载下来的 :" + authorConfig.toString();  
  11.     }  
  12. }  

到这里我们的spring-cloud 配置中心的客户端案例就完成了。
开始测试我们的案例: 一次启动下面三个项目 : eureka-sever, config-server,config-client-consumer; 在访问之前我们先看一下git 上的配置文件信息:
spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2


我们看看, 访问是否会加载到信息
访问: localhost:9093/configConsumer/getAuthorInfo
spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2

到这里我们spring-cloud config 分布式配置中心就完成了。
接下来我们讲解zuul的使用: 对于zuul 的依赖也是发生了变化, artifactId 已经变成了:
spring-cloud-starter-netflix-zuul 也是迁移netflix 下面了。
开始我们的案例:
  • 添加依赖
  1. <dependency>  
  2.     <groupId>org.springframework.cloud</groupId>  
  3.     <artifactId>spring-cloud-starter-netflix-zuul</artifactId>  
  4. </dependency>  
  5. <dependency>  
  6.     <groupId>org.springframework.cloud</groupId>  
  7.     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
  8. </dependency>  

  • 创建启动类 ZuulServiceApplication
  1. @SpringBootApplication  
  2. @EnableDiscoveryClient  
  3. @EnableZuulProxy  
  4. public class ZuulServiceApplication {  
  5.   
  6.     public static void main(String[] args) {  
  7.         SpringApplication.run(ZuulServiceApplication.class, args);  
  8.     }  
  9. }  


需要注意的是, 如果你不使用代理的话, 可以不使用@EnableZuulProxy, 而是使用@EnableZuulServer
  • 创建application.yml 文件 添加如下内容
  1. spring:  
  2.     application:  
  3.         name: zuul-service  
  4. server:  
  5.   port: 9094  
  6.   
  7. zuul:  
  8.   prefix: /api #为zuul设置一个公共的前缀  
  9.   routes:  
  10.     consumer:  
  11.       path: /consumer/**  
  12.       serviceId: eureka-client-consumer  
  13.     configConsumer:  
  14.       path: /configConsumer/**  
  15.       serviceId: config-client-consumer  
  16.         
  17. eureka:  
  18.   client:  
  19.     service-url:  
  20.            defaultZone: http://localhost:9871/eureka  


其中zuul部分为路由规则, prefix 为请求的前缀, routes 下一级节点, 可以*命名,
path 为请求路径, 后面的** 表示的是匹配后面的所有路径, 路由到相应的serviceId 这个服务上
上述的配置完成后我们依次启动项目eureka-server, config-server, config-client-consumer, eureka-client-provider,eureka-client-consumer 进行测试走zuul 路由,
访问下面的地址:
http://localhost:9094/api/configConsumer/configConsumer/getAuthorInfo
http://localhost:9094/api/consumer/company/companyInfo?companyName=springcloud.cn
同样会得到同样的返回结果:
spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2
spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2
接下来我们, 借助config 配置中心, 来改造一下zuul, 注册信息, 丛git 上拉取,
并且观察端点信息, 在新版的spring boot 2.x 端点信息的访问地址发生变化
在项目 zzf-spring-cloud-Finchley 项目下创建模块命名为:zuul-service-using-config
基础的配置和zuul-server 一样主要是多添加如下内容:
  • 添加额外依赖
  1. <dependency>  
  2.     <groupId>org.springframework.cloud</groupId>  
  3.     <artifactId>spring-cloud-starter-config</artifactId>  
  4. </dependency>  
  5. <!-- 做简单的安全和端点开放 -->  
  6. <dependency>  
  7.     <groupId>org.springframework.boot</groupId>  
  8.     <artifactId>spring-boot-starter-security</artifactId>  
  9. </dependency>  

  • 创建文件 bootstrap.yml 添加如下内容
  1. spring:  
  2.     cloud:  
  3.         config:  
  4.             label: master  
  5.             uri: http://localhost:9092  
  6.             name: zuul-config,default  

# 需要注意的是 name : 后面的是是git 上面的文件名称
  • 修改端口为9095
  1. server:  
  2.   port: 9095  

  • 在application.properties 文件中添加如下内容, 主要是用于打开所有的端点信息默认只有info、health 和详细信息的观察
  • 还需创建文件用于过滤和开发端点:
  1. @Configuration  
  2. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {  
  3.   
  4.    @SuppressWarnings("deprecation")  
  5.    @Bean  
  6.    public InMemoryUserDetailsManager inMemoryUserDetailsManager() {  
  7.       return new InMemoryUserDetailsManager(  
  8.            User.withDefaultPasswordEncoder().username("user").password("password")  
  9.                   .authorities("ROLE_USER").build(),  
  10.             User.withDefaultPasswordEncoder().username("admin").password("admin")  
  11.                   .authorities("ROLE_ACTUATOR""ROLE_USER").build());  
  12.    }  
  13.   
  14.    @Override  
  15.    protected void configure(HttpSecurity http) throws Exception {  
  16.       http  
  17.             .authorizeRequests()  
  18.             .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")  
  19.             .antMatchers("/**").permitAll()  
  20.             .and()  
  21.             .httpBasic();  
  22.    }  
  23. }  


到这里我们就改成完成了, 为什么要改成这样的案例呢?是因为在实际开发和线上, 都是需要动态修改配置的相关信息, 改造这个小案例是为了更符合实际。
我们在启动, 这个项目, 关闭zuul-server 项目, 在试试上面的访问地址,
端点地址: http://localhost:9095/actuator/routes
自己可以根据自己的需要访问端点信息。
http://localhost:9095/api/configConsumer/configConsumer/getAuthorInfo
spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读2

同样访问成功, 后面的问题集和源码地址请看后续的文章。 继续观看地址为: spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读3