父应用
- GroupId: top.bing
- Artifact: battle
- Dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- lombok 工具通过在代码编译时期动态的将注解替换为具体的代码, IDEA 需要添加 lombok 插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 标识 SpringCloud 的版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 配置远程仓库 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
服务发现
服务发现服务端
- 新建应用
- Artifact: discovery-server
- Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- 开启 Eureka Server
- application.yml 配置
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
- 启动 Eureka Server
服务发现客户端
- 新建应用
- Artifact: discovery-client
- Dependencies:
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
- 开启 Eureka Client
- application.yml 配置
spring:
application:
name: discovery-client
management:
endpoints:
web:
exposure:
include: shutdown
endpoint:
shutdown:
enabled: true
server:
port: 8080
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka/
- 启动 Eureka Client
配置管理
配置管理服务
- 新建应用
- Artifact: config-server
- Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-actuator</artifactId>
</dependency>
</dependencies>
- 开启配置管理服务
- application.yml 配置
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/config/
management:
endpoints:
web:
exposure:
include: shutdown
endpoint:
shutdown:
enabled: true
server:
port: 8888
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka/
- 配置文件
- ./config/**
- discovery-client.yml
server:
port: 9997
- discovery-client-dev.yml
- discovery-client-prod.yml
- 启动配置管理服务
配置客户端
- 添加 Config Client 依赖
- 添加 bootstrap.yml
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
- 使用远程默认配置启动
- 以 dev Profile 启动
- 以 prod Profile 启动
同步服务交互
服务提供者
- 添加领域模型 Person
public class Person {
private Long id;
private String name;
private Integer age;
private Integer serverPort;
}
- 添加演示控制器
@RestController
@RequestMapping("/people")
public class PersonController {
@Value("${server.port}")
Integer port;
@PostMapping
public Person save(@RequestBody Person person){
person.setServerPort(port);
return person;
}
@GetMapping("/{id}")
public Person getOne(@PathVariable Long id){
return new Person(id, "wyf", 35 , port);
}
@GetMapping("/findByAge")
public List<Person> findByAge(@RequestParam Integer age){
return Arrays.asList(new Person(1l, "wyf", 35 , port),
new Person(2l, "foo", 35 , port));
}
@DeleteMapping("/{id}")
public String delete(@PathVariable Long id){
return "从端口为:" + port + "的服务删除";
}
}
- 启动 discovery-client
服务使用者
- 新建应用
- Artifact: feign-client
- Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
- application.yml 配置
spring:
application:
name: feign-client
management:
endpoints:
web:
exposure:
include: shutdown
endpoint:
shutdown:
enabled: true
server:
port: 8082
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka/
- 开启 Feign Client
- Feign 客户端声明
@FeignClient("discovery-client")
public interface PersonClient {
@PostMapping("/people")
Person save(@RequestBody Person person);
@GetMapping("/people/{id}")
Person getOne(@PathVariable("id") Long id);
@GetMapping("/people/findByAge")
List<Person> findByAge(@RequestParam("age") Integer age);
@DeleteMapping("/people/{id}")
String delete(@PathVariable("id") Long id);
}
public class Person {
private Long id;
private String name;
private Integer age;
private Integer serverPort;
public Person(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
- 使用 Feign 客户端
@Bean
CommandLineRunner personClientClr(PersonClient personClient){
return args -> {
System.out.println(personClient.save(new Person(1l, "wyf", 35)));
System.out.println(personClient.getOne(1l));
personClient.findByAge(35).forEach(System.out::println);
System.out.println(personClient.delete(1l));
};
}
异步服务交互
person-service
- 新建应用
- 开启支持
- application.yml 配置
- 领域模型
- 数据访问 Repository
- 事件发送
- 事件接收
- 验证控制器
- 正常启动应用
address-service
- 新建应用
- 开启支持
- application.yml 配置
- 领域模型
- 数据访问 Repository
- 事件发送
- 事件接收
- 验证控制器
- 正常启动应用
应用网关
- 新建应用
- Artifact: gateway
- Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</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-actuator</artifactId>
</dependency>
</dependencies>
- 在“配置管理”服务进行配置 在 config-server 的 src/main/resources/config 目录下,添加gateway.xml
server:
port: 81
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: discoveryClient
uri: lb://discovery-client
predicates:
- Path=/dis/**
filters:
- RewritePath=/dis/(?<segment>.*), /$\\{segment}
- id: uaa
uri: lb://auth-server
predicates:
- Path=/uaa/**
filters:
- RewritePath=/uaa/(?<segment>.*), /$\\{segment}
- id: resource1
uri: lb://resource-server
predicates:
- Path=/res/**
filters:
- RewritePath=/res/(?<segment>.*), /$\\{segment}
- id: feign
uri: lb://feign-service
predicates:
- Path=/fgn/**
filters:
- RewritePath=/fgn/(?<segment>.*), /$\\{segment}
- 开启 Eureka 客户端
- application.yml
spring:
application:
name: gateway
management:
endpoints:
web:
exposure:
include: shutdown
endpoint:
shutdown:
enabled: true
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka/
- bootstrap.yml
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
- 验证
- 修改默认
来源:oschina
链接:https://my.oschina.net/beanho/blog/3167674