Spring Cloud - 7 (Spring Cloud Zuul)

Zuul基本使用 


@EnableEurekaClient

@EnableDiscoveryClient

Nginx+Lua

Lua:控制规则(A/B Test)

Spring Cloud 学习技巧:

善于定位应用:Feign、ConfigServer、Eureka、Zuul、Ribbon定位应用,配置方式是不同

增加@EnableZuulProxy

@SpringBootApplication
@EnableZuulProxy
public class SpringCloudZuulDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudZuulDemoApplication.class, args);
    }

}

增加@EnableZuulProxy

配置路由规则

基本模式:

zuul.routes.${app-name} = /${app-url-prefix}/**

整合Ribbon 


启动应用

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 

整合Eureka


引入spring-cloud-starter-eureka依赖

<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

整合Hystrix


在person-service服务端

激活Hystrix

@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
public class FeignServiceBootStrapApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignServiceBootStrapApplication.class, args);
    }

}

配置Hystrix规则

@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();
	}
}

整合Feign


服务消费端:person-client

调用链路

spring-cloud-zuul -> person-client -> person-service

person-cliect注册到EurekaService

端口信息:

  • spring-cloud-zuul:7070
  • person-client:8080
  • person-service:9090
  • Eureka Server:12345

启动person-client

@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();
    }

}

person-client注册到EurekaServer

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

网关应用:spring-cloud-zuul

增加路由器应用到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

整合Config Server


前面的例子展示了、Hystrix、Eureka以及Ribbon能力,可是配置相对是固定的,真实线上环境需要一个动态路由,即需要动态配置。

端口信息:

  • spring-cloud-zuul:7070
  • person-client:8080
  • person-service:9090
  • Eureka Server:12345
  • Config Server:10001

调整配置项

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

为spring-cloud-zuul增加配置文件

三个profile的配置文件

  • zuul.properties
  • zuul-test.properties
  • zuul-prod.properties

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仓库,否则添加以上三个文件也没有效果。

注册到Eureka服务器

<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

配置网关服务:spring-cloud-zuul

端口信息:

  • spring-cloud-zuul:7070
  • person-client:8080
  • person-service:9090
  • Eureka Server:12345
  • Config Server:10001

增加 spring-cloud-starter-config依赖

将之前:

zuul.routes.person-service
zuul.routes.person-client

配置注释

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

创建bootstrap.properties

配置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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值