接上一篇html
http://www.javashuo.com/article/p-hncxlqxm-gc.htmljava
服務端新增引入依賴web
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.1.RELEASE</version> </dependency>
啓動類開啓服務註冊支持spring
@EnableSwagger2 @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
增長服務註冊地址 application.propertiesdocker
spring.cloud.nacos.discovery.server-addr=192.168.50.31:8848
消費端POM.XML以下,引入ribbon與openfeign支持apache
<?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>
建立服務引用對象,利用FeignClientapp
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(); }
調用服務方法maven
@RestController @RequestMapping("/configConsumer") public class ConfigController { @Autowired private ICustomerService customerService; @RequestMapping(value = "/get", method = RequestMethod.GET) @ResponseBody public String get() { return customerService.get(); } }
提供服務發現ide
@EnableSwagger2 @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
啓動測試是否可以調用spring-boot