@EnableEurekaClient
@EnableDiscoveryClient
Nginx+Lua
Lua:控制规则(A/B Test)
Spring Cloud 学习技巧:
善于定位应用:Feign、ConfigServer、Eureka、Zuul、Ribbon定位应用,配置方式是不同
@SpringBootApplication
@EnableZuulProxy
public class SpringCloudZuulDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZuulDemoApplication.class, args);
}
}
配置路由规则
基本模式:
zuul.routes.${app-name} = /${app-url-prefix}/**
spring-cloud-eureka-server
person-service
zuul->person-service
##Zuul 服务端口
server.port = 7070
##Zuul 基本配置模式
#zuul.routes.${app-name}:/{app-url-prefix}/**
##Zuul 配置 person-service 服务调用
zuul.routes.person-service = /person-service/**
##Ribbon取消Eureka整合
ribbon.eureka.enabled = false
## 配置 "person-service" 的负载均衡服务器列表
person-service.ribbon.listOfServers = \
http://localhost:9090
注意:http:localhost:7070/person-service/person/find/all
person-service的app-url-prefix:/person-service/
person-service的具体URI:/person/find/all
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class SpringCloudZuulDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZuulDemoApplication.class, args);
}
}
## Eureka Server服务URL,用于客户端注册
##Zuul 服务端口
server.port = 7070
##Zuul 基本配置模式
#zuul.routes.${app-name}:/{app-url-prefix}/**
##Zuul 配置 person-service 服务调用
zuul.routes.person-service = /person-service/**
##Ribbon取消Eureka整合
#ribbon.eureka.enabled = false
## 配置 "person-service" 的负载均衡服务器列表
#person-service.ribbon.listOfServers = \
#http://localhost:9090
eureka.client.serviceUrl.defaultZone=http://localhost:12345/eureka
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
public class FeignServiceBootStrapApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServiceBootStrapApplication.class, args);
}
}
@RestController
public class PersonServiceController implements PersonService{
private final ConcurrentHashMap<Long,Person> map = new ConcurrentHashMap<Long, Person>();
@PostMapping("/person/save")//实现了PersonService接口后可以不用写mapping地址
@Override
public boolean save(@RequestBody Person person) {
return map.put(person.getId(), person) == null;
}
@HystrixCommand(defaultFallback= "errorContent",commandProperties =
{@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value= "100")})
@GetMapping("/person/findall")
@Override
public Collection<Person> findAll() {
Random random = new Random();
int nextInt = random.nextInt(200);
System.err.println("random time > "+nextInt);
try {
Thread.sleep(nextInt);
} catch (InterruptedException e) {
e.printStackTrace();
}
return map.values();
}
public Collection<Person> errorContent() {
System.out.println("超时了");
return Collections.emptyList();
}
}
调用链路
spring-cloud-zuul -> person-client -> person-service
端口信息:
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(clients = PersonService.class)
@RibbonClient(value = "person-service",configuration = MyRule.class)
@EnableHystrix
public class FeignClientBootStrapApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientBootStrapApplication.class, args);
}
@Bean
public MyRule myRule() {
return new MyRule();
}
}
spring.application.name=person-client
server.port=8080
management.endpoints.web.exposure.include=*
##eureka.client.register-with-eureka=false
ribbon.eureka.enabled = false
eureka.client.serviceUrl.defaultZone=http://localhost:12345/eureka
增加路由器应用到person-client
## Zuul配置 person-client服务调用
zuul.routes.person-client = /person-client/**
http://localhost:7070/person-client/person/findall
spring-cloud-zuul(7070) -> person-client(8080) -> person-service(9090)
等价的Ribbon(不走注册中心)
##Ribbon取消Eureka整合
#ribbon.eureka.enabled = false
## 配置 "person-service" 的负载均衡服务器列表
#person-service.ribbon.listOfServers = \
#http://localhost:9090
#person-service.ribbon.listOfServers = \
#http://localhost:8080
前面的例子展示了、Hystrix、Eureka以及Ribbon能力,可是配置相对是固定的,真实线上环境需要一个动态路由,即需要动态配置。
端口信息:
spring.application.name = config-server
server.port=10001
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.config.server.git.uri=file:///${user.dir}/src/main/resources/configs
三个profile的配置文件
zuul.properties
## 应用 spring-cloud-zuul 默认配置项(profile = 'default')
##Zuul 基本配置模式
#zuul.routes.${app-name}:/{app-url-prefix}/**
##Zuul 配置 person-service 服务调用
zuul.routes.person-service = /person-service/*
##Zuul 配置 person-client 服务调用
#zuul.routes.person-client = /person-client/**
zuul-test.properties
## 应用 spring-cloud-zuul 默认配置项(profile='test')
##Zuul 基本配置模式
#zuul.routes.${app-name}:/{app-url-prefix}/**
##Zuul 配置 person-service 服务调用
#zuul.routes.person-service = /person-service/**
##Zuul 配置 person-client 服务调用
zuul.routes.person-client = /person-client/**
zuul-prod.properties
## 应用 spring-cloud-zuul 默认配置项(profile='prod')
##Zuul 基本配置模式
#zuul.routes.${app-name}:/{app-url-prefix}/**
##Zuul 配置 person-service 服务调用
zuul.routes.person-service = /person-service/**
##Zuul 配置 person-client 服务调用
zuul.routes.person-client = /person-client/**
初始化${user.dir}/src/main/resources/configs为git根目录
1. 初始化
$ git init
Initialized empty Git repository in D:/workspaces/spring-cloud-config-server-demo/src/main/resources/configs/.git/
2. 增加上述三个文件到git仓库
$ git add *.properties
3. 提交到本地git仓库
$ git commit -m "初始化提交"
[master (root-commit) 7953d2c] 初始化提交
3 files changed, 36 insertions(+)
create mode 100644 zuul-prod.properties
create mode 100644 zuul-test.properties
create mode 100644 zuul.properties
注:以上操作为了让Spring Cloud Git配置服务器识别Git仓库,否则添加以上三个文件也没有效果。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
## Eureka Server 服务URL ,用户客户端注册
eureka.client.serviceUrl.defaultZone=http://localhost:12345/eureka
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class SpringCloudConfigServerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerDemoApplication.class, args);
}
}
http://localhost:10001/zuul/default
http://localhost:10001/zuul/test
http://localhost:10001/zuul/prod
端口信息:
将之前:
zuul.routes.person-service
zuul.routes.person-client
配置注释
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
配置config客户端信息
### bootstrap 上下文配置
#配置客户端应用:zuul
spring.cloud.config.name=zuul
#profile是激活配置
spring.cloud.config.profile=prod
#label 在git中指定分支名
spring.cloud.config.lable=master
#采用Discovery client 连接方式
## 激活discovery 连接配置项的方式
spring.cloud.config.discovery.enabled = true
# 配置 config server 应用名称
spring.cloud.config.discovery.serviceId = spring-cloud-config-server
http://localhost:7070/person-client/person/findall
spring-cloud-zuul -> person-client ->person-service
http://localhost:7070/person-service/person/findall
spring-cloud-zuul -> person-service