创建2个微服务工程 service-provider,service-customer,一个父工程 nacos-micro
父工程的 pom 文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.xielong</groupId> <artifactId>nacos-micro</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>service-provider</module> <module>service-customer</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring.boot.version>2.1.0.RELEASE</spring.boot.version> <spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.version}</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>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
service-provider
bootstrap.yml
spring: cloud: nacos: discovery: server-addr: http://nacos.yaotiao.net/login namespace: 6609e34a-ac49-48b6-98a9-58daf54b5057 group: TEST_GROUP config: server-addr: http://nacos.yaotiao.net/login namespace: 6609e34a-ac49-48b6-98a9-58daf54b5057 group: TEST_GROUP shared-configs[0]: data-id: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} group: TEST_GROUP refresh: true
ProviderApplication
package cn.xielong; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }
ProviderController
package cn.xielong.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProviderController { @Value("${common.name}") private String configs; @RequestMapping("/configs") public String get() { return configs; } }
service-customer
bootstrap.yml
server: port: 8090 spring: application: name: customer cloud: nacos: discovery: server-addr: 192.168.2.103:8848 namespace: 0963f5f1-3d09-4dd5-87a8-6d4ce66717f5 config: server-addr: 192.168.2.103:8848 file-extension: yaml group: DEFAULT_GROUP namespace: 0963f5f1-3d09-4dd5-87a8-6d4ce66717f5
CustomerApplication
package cn.xielong; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class CustomerApplication { public static void main(String[] args) { SpringApplication.run(CustomerApplication.class, args); } }
CustomerController
package cn.xielong.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class CustomerController { @Value("${common.name}") private String configs; @RequestMapping("/configs") public String get() { return configs; } }
配置列表
服务发现