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
接下来我们开始一个新的模块: 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
相关文章推荐
- spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读
- spring-boot2.0 + spring cloud 的Finchley版的 M7 新用法解读3
- 基于SpringBoot 2.0正式版的SpringCloud的微服务实战项目搭建
- Spring Boot 2.0 M7 整合 ES 5 、Kibana 和 X-pack
- SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)
- spring cloud项目升级spring boot 2.0爬过的坑
- spring boot / cloud (十) 使用quartz搭建调度中心
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十六)使用Redis做集中式缓存(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(八)开发Web应用(2)
- Spring boot 2.0 with Oauth2 + Jwt
- Spring cloud eureka+Client+Spring boot admin 服务注册监控
- SpringBoot2.0 集成Reids,作为数据库或者缓存。并支持CRUD
- 【译】Spring Boot 2.0 官方迁移指南
- spring boot and spring cloud 组件
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十六)使用RabbitMQ(1)
- Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收藏
- SpringCloud SpringBoot mybatis 分布式微服务(十七)Spring Boot中使用LDAP来统一管理用户信息
- spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的?
- 【2.0新特性】Spring Boot 2.0新特性
- SpringBoot2.0启动报错