# spring-cloud-in-action
**Repository Path**: liuyazong/spring-cloud-in-action
## Basic Information
- **Project Name**: spring-cloud-in-action
- **Description**: spring cloud in action
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2018-03-25
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 服务注册中心
添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
启动类添加注解
@EnableEurekaServer
配置文件bootstrap.yml
详细配置项见`org.springframework.cloud.netflix.eureka.EurekaClientConfigBean`
# 服务提供者
添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
启动类添加注解
在服务启动类添加注解。非必要,只要classpath下有DiscoveryClient的实现,Spring Boot就会自动注册。
Spring会寻找DiscoveryClient的实现,支持的实现有
* [Spring Cloud Netflix Eureka](https://cloud.spring.io/spring-cloud-netflix/)
* [Spring Cloud Consul Discovery](https://cloud.spring.io/spring-cloud-consul/)
* [Spring Cloud Zookeeper Discovery](https://cloud.spring.io/spring-cloud-zookeeper/)
@EnableDiscoveryClient
配置文件bootstrap.yml
详细配置项见`org.springframework.cloud.netflix.eureka.EurekaClientConfigBean`
# 配置中心
添加依赖
org.springframework.cloud
spring-cloud-config-server
启动类添加注解
@EnableConfigServer
配置文件bootstrap.yml
详细配置项见`org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository`
# 路由
添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
启动类添加注解
@EnableZuulProxy
配置文件bootstrap.yml
详细配置项见`org.springframework.cloud.netflix.zuul.filters.ZuulProperties`
# 服务消费者
## ribbon
添加依赖
org.springframework.cloud
spring-cloud-starter-ribbon
客户端声明
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
## feign
添加依赖
org.springframework.cloud
spring-cloud-starter-feign
启动类添加注解
@EnableFeignClients
客户端声明,使用时直接注入即可
@FeignClient("service-hello")
public interface HelloClient {
@GetMapping("hello/{sth}")
String hello(@PathVariable("sth") String sth);
}
# 断路器
添加依赖
org.springframework.cloud
spring-cloud-starter-hystrix
启动类添加注解
@EnableCircuitBreaker
添加断路器
@Service
public class HelloService {
@Autowired
private HelloClient helloClient;
@HystrixCommand(fallbackMethod = "helloError")
public String hello(String sth) {
return helloClient.hello(sth);
}
public String helloError(String sth) {
return String.format("hello, error, %s", sth);
}
}
# 服务监控
新建spring boot工程,并添加如下依赖
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-turbine
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
启动类添加注解
@EnableHystrixDashboard
@EnableTurbine
配置,详细配置项见`org.springframework.cloud.netflix.turbine.TurbineProperties`
server:
port: 2333
turbine:
app-config: service-hello,service-hi
aggregator:
clusterConfig: default
clusterNameExpression: default
并确保`app-config`所包含的服务启用`@EnableCircuitBreaker`注解
启动应用,浏览器输入`http://localhost:2333/hystrix`即可看到监控界面