写点什么

SpringCloud 学习之【NACOS 实现服务的注册与发现】,kafka 基础原理

用户头像
极客good
关注
发布于: 刚刚

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


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


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);


}


另外是RestTemplateWebClient.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 管理页面,服务也正常注册:



访问

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
SpringCloud学习之【NACOS实现服务的注册与发现】,kafka基础原理