前言
官方案例的笔记
Nacos官方文档#启动服务发现
时间线——2021-04-19—
启动报错:hibernate-validator
,去掉 pom.xml
的 hibernate-validator
,去掉 spring-boot-starter-web
中的 hibernate-validator
1 2 3 4 5 6 7 8 9 10
| ><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>hibernate-validator</artifactId> <groupId>org.hibernate.validator</groupId> </exclusion> </exclusions> </dependency>
|
实现
依赖
provider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>hibernate-validator</artifactId> <groupId>org.hibernate.validator</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.2.RELEASE</version> <exclusions> <exclusion> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.1.0</version> </dependency> </dependencies>
|
consumer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>hibernate-validator</artifactId> <groupId>org.hibernate.validator</groupId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>${spring-cloud-netflix.version}</version> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>${spring-cloud-openfeign.version}</version> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <exclusion> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> </dependencies>
|
版本说明 Wiki
打不开 github
的版本参考Nacos启动配置管理-SpringCloud(1)
配置
provider:port=8070
1 2 3
| server.port=8070 spring.application.name=service-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
consumer:port=8080
1 2 3
| server.port=8080 spring.application.name=service-consumer spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
代码
provider
通过 Spring Cloud
原生注解 @EnableDiscoveryClient
开启服务注册发现功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @SpringBootApplication @EnableDiscoveryClient public class NacosProviderApplication {
public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); }
@RestController class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return "Hello Nacos Discovery " + string; } } }
|
consumer
通过 Spring Cloud
原生注解 @EnableDiscoveryClient
开启服务注册发现功能。给 RestTemplate 实例添加 @LoadBalanced
注解,开启 @LoadBalanced
与 Ribbon 的集成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @SpringBootApplication @EnableDiscoveryClient public class NacosConsumerApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); }
@RestController public class TestController {
private final RestTemplate restTemplate;
@Autowired public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) public String echo(@PathVariable String str) { return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); } } }
|
测试
启动 ProviderApplication
和 ConsumerApplication
,调用 http://localhost:8080/echo/2018
,返回内容为 Hello Nacos Discovery 2018
。
本文地址: https://github.com/maxzhao-it/blog/post/49625/