SpringCloud应用集成Nacos服务注册与发现
第一步:引入依赖
版本见 => 附录:根pom文件=>版本控制片段 www.cnblogs.com/anhaoyan...
<!-- 引入nacos用于服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Spring Boot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入openfeign用于服务消费测试 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
第二步:配置 application.yml 文件
(1)服务提供者
server:
port: 8083
spring:
application:
name: spring-cloud-alibaba-nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
(2)服务消费者
server:
port: 8082
spring:
application:
name: spring-cloud-alibaba-nacos-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
第三步:启动类加@EnableDiscoveryClient
注解开启服务注册与发现
(1)服务提供者
package com.anhaoyang.test.naocs.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //开启服务发现
public class BootNacosDiscoveryProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootNacosDiscoveryProviderApplication.class, args);
}
}
package com.anhaoyang.test.naocs.provider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* 服务提供测试类
**/
@RestController
public class ProviderController {
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "Provider received: " + string;
}
}
(2)服务消费者
package com.anhaoyang.test.naocs.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient //开启服务发现
public class BootNacosDiscoveryConsumerApplication {
/**
* 创建RestTemplate用于测试
* @return
*/
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(BootNacosDiscoveryConsumerApplication.class, args);
}
}
package com.anhaoyang.test.naocs.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* 服务消费测试
**/
@RestController
public class ConsumerController {
private final RestTemplate restTemplate;
@Autowired
public ConsumerController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/get")
public String get() {
return restTemplate.getForObject("http://spring-cloud-alibaba-nacos-provider/echo/" + "ConsumerRequestBody", String.class);
}
}
第四步:验证
(1)启动服务后,Nacos控制台-服务列表
(2)服务消费者-> 访问rpc结果,成功。
标签:集成,spring,SpringCloud,Nacos,springframework,org,import,public,cloud From: https://www.cnblogs.com/anhaoyang/p/springcloud-application-integrates-nacos-service-regis