SpringCloud 学习之【NACOS 实现服务的注册与发现】,kafka 基础原理
public class TestController {
private static Logger log = LoggerFactory.getLogger(TestController.class);
@GetMapping("/hello")
public String hello(@RequestParam String name) {
log.info("invoked name = " + name);
return "hello " + name;
}
}
新增配置:
应用名
spring.application.name=alibaba-nacos-discovery-server
端口
server.port=8001
Nacos 服务注册中心地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
启动:
INFO 25148 --- [ main] o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, alibaba-nacos-discovery-server 192.168.10.211:8001 register finished
访问 nacos 管理页面:
[](
)服务消费方
目录结构如下:
pom.xml 依赖配置如以下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
启动类增加@EnableDiscoveryClient
注解,开启 Spring Cloud 的服务注册与发现,添加@EnableFeignClients
注解,用于 controller 里多种调用方式演示
@EnableDiscoveryClient
@SpringBootApplication
public class AlibabaNacosDiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosDiscoveryServerApplication.class, args);
}
}
提供接口服务TestController
,提供多种服务调用方式,分别为 Feign、RestTemplate、WebClient:
@RestController
public class TestController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@Autowired
private WebClient.Builder webClientBuilder;
@Autowired
private TestFeignClient client;
/* original */
@GetMapping("/test")
public String test() {
// 通过 spring cloud common 中的负载均衡接口选取服务提供节点实现接口调用
ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
String url = serviceInstance.getUri() + "/hello?name=" + "gaohb";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
return "Invoke : " + url + ", return : " + result;
}
/* feign */
@GetMapping("/testFeign")
public String testFeign() {
String result = client.hello("gaohb");
return "Return : " + result;
}
/* RestTemplate */
@GetMapping("/testRestTemplate")
public String testRestTemplate() {
String result = restTemplate.getForObject("http://alibaba-nacos-discovery-se
rver/hello?name=gaohb", String.class);
return "Return : " + result;
}
/* webClient */
@GetMapping("/testWebClient")
public Mono<String> testWebClient() {
Mono<String> result = webClientBuilder.build()
.get()
.uri("http://alibaba-nacos-discovery-server/hello?name=gaohb")
.retrieve()
.bodyToMono(String.class);
return result;
}
}
新增 feign 客户端TestFeignClient
,用于声明式调用服务演示:
@FeignClient("alibaba-nacos-discovery-server")
public interface TestFeignClient {
@GetMapping("/hello")
String hello(@RequestParam(name = "name") String name);
}
另外是RestTemplate
和 WebClient.Builder
注册到 Spring 容器,用于 RestTemplate 和 WebClient.Builder 两种服务调用方式演示
@Configuration
public class TestConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}
新增配置:
应用名
spring.application.name=alibaba-nacos-discovery-client-common
端口
server.port=9000
Nacos 服务注册中心地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
启动:
INFO 25148 --- [ main] o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, alibaba-nacos-discovery-common 192.168.10.211:9000 register finished
访问 nacos 管理页面,服务也正常注册:
访问
评论