一 服务提供配置
- pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--nacos 一定要注意版本问题--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.9.0.RELEASE</version> </dependency> </dependencies>
- application.yml
server: port: 8881 spring: application: name: nacos-product cloud: nacos: discovery: server-addr: 192.168.180.113:8848
- 启动类
@SpringBootApplication @EnableDiscoveryClient public class NacosProductApplication { ? public static void main(String[] args) { SpringApplication.run(NacosProductApplication.class, args); } ? }
- web层
1 /** 2 * @author WGR 3 * @create 2019/10/25 -- 0:29 4 */ 5 @RestController 6 @RequestMapping("/user") 7 public class UserController { 8 @GetMapping 9 public String getUser(){ 10 return "admin"; 11 } 12 }
测试结果:
二 提供消费配置
- pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ? <!--nacos 一定要注意版本问题--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
- application.yml
server: port: 8882 spring: application: name: nacos-consumer cloud: nacos: discovery: server-addr: 192.168.180.113:8848
- 启动类配置
@SpringBootApplication @EnableFeignClients public class NacosConsumerApplication { ? public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } ? } ?
- web层
/** * @author WGR * @create 2019/10/25 -- 0:46 */ @RestController @RequestMapping("/test") public class TestController { ? @Autowired(required = false) private TestService testService; ? @GetMapping("/user") public String getUser() { return testService.getUser(); } }
- service层
/** * @author WGR * @create 2019/10/25 -- 0:47 */ @FeignClient("nacos-product") public interface TestService { ? @GetMapping("user") String getUser(); ? }
?
原文地址:https://www.cnblogs.com/dalianpai/p/11735937.html
时间: 2024-11-01 23:58:18