SpringCloud + Eureka / Nacos
项目目录:
父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>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>api</module>
<module>consumer-eureka</module>
<module>consumer-nacos</module>
<module>provider-eureka</module>
<module>provider-nacos</module>
<module>eureka-server</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</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-alibaba-nacos-config</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
基于Eureka的服务注册和发现 provider-eureka
目录
pom.xml
<?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>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.provider.eureka</groupId>
<artifactId>provider-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-eureka</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 7070
spring:
application:
name: provider-eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
ProviderEurekaApplication
@SpringBootApplication
@EnableEurekaClient
public class ProviderEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderEurekaApplication.class, args);
}
}
EchoServiceController
@RestController
@RequestMapping("/pe")
public class EchoServiceController {
@Autowired
private EchoService echoService;
@GetMapping("/echo")
public String echo(@RequestParam String name) {
return echoService.echo(name);
}
}
结果
基于Eureka的服务注册和发现 consumer-eureka
目录
pom.xml
<?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>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.consumer.eureka</groupId>
<artifactId>consumer-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer-eureka</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 7000
spring:
application:
name: consumer-eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
ConsumerEurekaApplication
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class ConsumerEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerEurekaApplication.class, args);
}
}
InvokeEchoServiceController
@RestController
@RequestMapping("/ce")
public class InvokeEchoServiceController {
@Autowired
private HelloController helloController;
@GetMapping("/echo")
public String echo(@RequestParam String name) {
return helloController.hello(name);
}
}
HelloController
@FeignClient(value = "provider-eureka")
public interface HelloController {
@GetMapping("/pe/echo")
String hello(@RequestParam(value = "name") String name);
}
结果
基于Nacos的服务注册和发现和配置中心 provider-nacos
目录
pom.xml
<?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>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.provider.nacos</groupId>
<artifactId>provider-nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-nacos</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.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>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
</project>
bootstrap.properties
spring.application.name=provider-nacos
server.port=9090
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
application.properties 空着,配置中心只能写在bootstrap.properties中,不然报错
ProviderNacosApplication
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderNacosApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderNacosApplication.class, args);
}
}
EchoController
@RestController
@RequestMapping("/pn")
@RefreshScope
public class EchoController {
@Value("${sys.user.name:null}")
private String user;
@GetMapping(value = "/hello")
public String hello(@RequestParam String name) {
return user + "Hello : " + name;
}
@GetMapping(value = "/echo")
public String echo(@RequestParam String name) {
return user + "Echo : " + name;
}
}
结果
基于Nacos的服务注册和发现和配置中心 consumer-nacos
目录
bootstrap.properties
server.port=9000
spring.application.name=consumer-nacos
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
ConsumerNacosApplication
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerNacosApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerNacosApplication.class, args);
}
}
EchoControllerCn
@RestController
@RequestMapping("/cn")
@RefreshScope
public class EchoControllerCn {
private final RestTemplate restTemplate;
@Value("${sys.user.name:null}")
private String user;
@Autowired
public EchoControllerCn(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping(value = "/hello")
public String echo(@RequestParam String name) {
return restTemplate.getForObject("http://nacos-provider/pn/hello?name=" + name, String.class);
}
@Autowired
private HelloController2 helloController2;
@Autowired
private HelloController helloController1;
@GetMapping(value = "/hello1")
public String echo1(String name) {
return user + helloController1.hello(name);
}
@GetMapping(value = "/hello2")
public String echo2(String name) {
return user + helloController2.hello(name);
}
}
HelloController
@FeignClient(value = "provider-nacos")
public interface HelloController {
@GetMapping("/pn/hello")
String hello(@RequestParam(value = "name") String name);
}
结果
Nacos 服务器
nacos多环境配置
参考:https://www.cnblogs.com/didispace/p/10340685.html#undefined
修改配置文件
nacos.config-dev.properties:
sys.user.name=user-dev
nacos.config-test.properties:
sys.user.name=user-test
指定配置文件前缀
spring.cloud.nacos.config.prefix=nacos.config
添加启动参数
-Dspring.profiles.active=test