您的位置:首页 > 编程语言 > Java开发

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

2018-03-08 11:25 701 查看
建议先看第一篇:spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读
接下来我们开始一个新的模块: config 分布式配置中心模块在项目 zzf-spring-cloud-Finchley 项目下创建模块命名为: config-server这个是配置中心的服务端。先是添加依赖:
<dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-config-server</artifactId>  
</dependency>  

创建启动类:
@EnableConfigServer  
@SpringBootApplication  
public class ConfigServerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(ConfigServerApplication.class, args);  
    }  
}  

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

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




服务端搭建完成后我, 我们开始创建客户端:在项目 zzf-spring-cloud-Finchley 项目下创建模块命名为: config-client-consumer先是添加依赖:
<dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-config</artifactId>  
</dependency>  
  
<dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
</dependency>  
  
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-web</artifactId>  
</dependency>  

创建ConfigClientConsumerApplication 类
@SpringBootApplication  
@EnableDiscoveryClient  
public class ConfigClientConsumerApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(ConfigClientConsumerApplication.class, args);  
    }  
}  

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

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

# 需要注意的是, author 就是git 上配置的文件名称
创建一个配置author 信息加载的类: AuthorConfig
@Component  
@ConfigurationProperties(prefix = "spring.zzf")  
public class AuthorConfig {  
  
    private String name;  
    private Integer age;  
    private Integer sex;  
   
    .....  
}  

这里省列get set 方法。
创建controller 类:
@Controller  
@RequestMapping("configConsumer")  
public class ConfigConsumerController {  
  
    @Autowired  
    private AuthorConfig authorConfig;  
    @RequestMapping("/getAuthorInfo")  
    @ResponseBody  
    public String getAuthorInfo(){  
        return " author信息是丛git上加载下来的 :" + authorConfig.toString();  
    }  
}  

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


我们看看, 访问是否会加载到信息访问: localhost:9093/configConsumer/getAuthorInfo


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

创建启动类 ZuulServiceApplication
@SpringBootApplication  
@EnableDiscoveryClient  
@EnableZuulProxy  
public class ZuulServiceApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(ZuulServiceApplication.class, args);  
    }  
}  

需要注意的是, 如果你不使用代理的话, 可以不使用@EnableZuulProxy, 而是使用@EnableZuulServer 创建application.yml 文件 添加如下内容
spring:  
    application:  
        name: zuul-service  
server:  
  port: 9094  
  
zuul:  
  prefix: /api #为zuul设置一个公共的前缀  
  routes:  
    consumer:  
      path: /consumer/**  
      serviceId: eureka-client-consumer  
    configConsumer:  
      path: /configConsumer/**  
      serviceId: config-client-consumer  
        
eureka:  
  client:  
    service-url:  
           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/getAuthorInfohttp://localhost:9094/api/consumer/company/companyInfo?companyName=springcloud.cn同样会得到同样的返回结果:




接下来我们, 借助config 配置中心, 来改造一下zuul, 注册信息, 丛git 上拉取,并且观察端点信息, 在新版的spring boot 2.x 端点信息的访问地址发生变化在项目 zzf-spring-cloud-Finchley 项目下创建模块命名为:zuul-service-using-config基础的配置和zuul-server 一样主要是多添加如下内容:添加额外依赖
<dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-config</artifactId>  
</dependency>  
<!-- 做简单的安全和端点开放 -->  
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-security</artifactId>  
</dependency>  

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

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

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

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


同样访问成功, 后面的问题集和源码地址请看后续的文章。 继续观看地址为: spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息