spring cloud

配置管理服务器的配置
spring-cloud-config-server

加上
@EnableConfigServer 启用配置管理服务
@EnableDiscovertClient 启用发现服务的客户端

将配置文件放在github上
spring.cloud.config.server.git.url=https://github.com/mxz/spring-cloud-config-repo

资源库下放置配置文件


配置管理服务器的配置
spring-cloud-starter-config
创建bookstrap.properties 用来设定连接配置服务器
spring.application.name=web 应用的名称及配置文件的名称
spring.profiles.active=development 配置文件的后缀部分(web-development.yml)
spring.cloud.config.uri=http://localhost:8888
application.properties 会默认加载
如此就可以使用了
@Value("$(cloud.sample.msg)") String msg; 或 environment.getProperty("cloud.sample.msg",undefined)


实现在线更新
在使用更新配置的bean上使用@RefreshScope注解 更新配置文件后 curl -X POST http://localhost:9001/refresh 刷新使用的项目


更新所有客户端配置
使用事件总线更新所有客户端 spring-cloud-starter-bus-amqp (通过RabbitMQ使用消息分发的方法执行更新安装RabbitMQ服务器)
在配置管理服务器和所有连接配置服务器的客户端配置
spring.rabbitmq.addresses=amqp://192.168.1.214:5672
spring.rabbitmq.username=admin
spring.rabbitmq.username=password
然后使用更新 curl -X POST http://localhost:8888/bus/refresh 刷新配置器达到刷新所有客户端
curl -X POST http://localhost:8888/bus/refresh?destination=web:** 更新指定客户端






使用发现服务 (常规是WebService或SOAP方式由服务提供者对外报漏接口,消费者再对接口访问)

创建发现服务器
spring-cloud-starter-eureka-server
@EnableEurekaServer

使用发现服务客户端配置
spring-cloud-starter-eureka
@EnableDiscoveryClient
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka
eureka.instance.preferIpAddress = true

在bookstrap.properties 配置该应用在发现服务器中的唯一标识
spring.application.name=web

调用服务
使用动态路由和断路器
在客户端
spring-cloud-starter-zuul
spring-cloud-starter-hystrix
@EnableZuulProxy
@EnableHystrix

共享Rest资源
使用 bean上加@RepositoryRestResource(collectionResourceRel = "users", path="users")
此时就可以使用了
在data模块上 http://localhost:9000/users
在web模块 使用data的服务 http://localhost:9001/data/users

后台调用
@Autowired
RestRemplate restTemplate.getForObject("http://data/users/search", User.class, params)

也可以加入spring-cloud-starter-fegin @EnableFegnClients
创建接口调用data模块的findById方法
@FeignClient("data")
public interface UserClient {
@GetMapping("/users/{id}")
User findById(@RequestParam("id")Long id);
}


断路器功能

@Autowired @LoadBalanced
RestRemplate
@HystrixCommand(fallbackMethod = "断路时访问的method名称")
restTemplate.getForObject("http://data/users/search", User.class, params)


使用监控服务
spring-cloud-starter-hystrix-dashboard
创建一个主程序 @Controller @EnableHystrixDashboard
@RequestMapping("/")
public String home() {
return "forward:/hystrix";
}
进入http://localhost:7979/
添加http://...../hystrix.stream 进行监控

posted @ 2018-04-13 14:28  jojoworld  阅读(162)  评论(0编辑  收藏  举报