Alibaba Nacos 学习(一):Nacos介绍与安装
Alibaba Nacos 学习(二):Spring Cloud Nacos Config
Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现
Alibaba Nacos 学习(四):Nacos Docker
Alibaba Nacos 学习(五):K8S Nacos搭建,使用nfs
接上一篇
https://www.cnblogs.com/woxpp/p/11908262.html
服务端新增引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.1.RELEASE</version> </dependency>
启动类开启服务注册支持
@EnableSwagger2 @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
增加服务注册地址 application.properties
spring.cloud.nacos.discovery.server-addr=192.168.50.31:8848
消费端POM.XML如下,引入ribbon与openfeign支持
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>nacos.consumer</groupId> <artifactId>nacos.consumer</artifactId> <name>nacos.consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-boot.version>2.0.3.RELEASE</spring-boot.version> <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version> </properties> <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> <version>0.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency> </dependencies> </project>
创建服务引用对象,利用FeignClient
package example.dockertest.example.dockertest.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @FeignClient(value = "service-provider") public interface ICustomerService { @RequestMapping(value = "/config/get", method = RequestMethod.GET) @ResponseBody String get(); }
调用服务方法
@RestController @RequestMapping("/configConsumer") public class ConfigController { @Autowired private ICustomerService customerService; @RequestMapping(value = "/get", method = RequestMethod.GET) @ResponseBody public String get() { return customerService.get(); } }
提供服务发现
@EnableSwagger2 @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
启动测试是否能够调用