Spring system diagram
Version relationship
eureka practice
1 Parent project dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.14</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2 Build Eureka Server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone:
3 Build a service provider
<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>
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
spring:
application:
name: eureka-provider
server:
port: 9000
4 Build consumers
<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>
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eureka-consumer
server:
port: 9001
5 Service consumers
5.1 Use RestTemplate to make RPC calls
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
5.2 Real call
@GetMapping("/hello/{id}")
public String hello(@PathVariable String id){
return restTemplate.getForObject("http://EUREKA-PROVIDER/hello/"+id, String.class);
}
注意:
The @LoadBalanced annotation must be added (org.springframework.cloud.client.loadbalancer)
Cluster mode:
https://cloud.tencent.com/developer/article/1730733
Naco Practice
1 First we need to install Nacos Server
and you can choose Docker, but you must pay attention to the pitfalls here and need to expose three ports:
docker run -d -p 8848:8848 -p 9848:9848 -p 9849:9849 --name nacos -e "MODE=standalone" -e NACOS_AUTH_IDENTITY_KEY=serverIdentity -e NACOS_AUTH_IDENTITY_VALUE=security -e NACOS_AUTH_TOKEN=SecretKey012345678901234567890123456789012345678901234567890123456789 nacos/nacos-server
2 Parent POM
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--这里的版本一定要对应上 和下面的spring-cloud-alibaba-dependencies里的spring boot 版本 -->
<version>2.4.2</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3 service providers
server:
port: 8088
spring:
application:
name: ddd-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4 Serve consumers
server:
port: 8089
application:
name: ddd-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</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-loadbalancer</artifactId>
<version>3.0.1</version>
</dependency>
5 The calling method is the same as above, using the load-balanced RestTemplate to make the call
Nacos configuration management
1 Parent project dependencies
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${com.alibaba.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${com.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-dubbo -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>${com.dubbo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 因为在spring-cloud-dependencies里面是optional 所以这里我引入了 不引入会报错 StringUtils -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
2 Specific modules, introducing dependencies
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
3. Configure bootstrap.yml
and write application.yml above. It is said that it can also be used in higher versions, but I did not succeed. I will test it later to supplement it.
spring:
application:
name: nacos-dynamic-port
cloud:
nacos:
config:
server-addr:ip:8848
file-extension: yml #这个名字可以自定义 与 配置列表对应就行 官网也有一套命名规则
namespace: 459c3b94-04e8-4de2-8eaa-c55f4a8de1b1 #这里需填工作空间的ID
group: DEFAULT_GROUP
Naming rules:
Configuration list:
4 Verify that you can modify the port in nacos, then start it and find that the port you configured will take effect:
@RestController
@RefreshScope 、// 这个注解别忘了这个提供了bean刷新属性的能力
public class ConfigController {
@Value("${server.port}")
private int port;
@RequestMapping("/info")
public String get() {
return "NacosDynamicPortController output, port: " + port;
}
}
Modify different ports and find that info will print different ports.但是特别注意哈我们一般不用来更新端口,因为这个端口改了,但是服务还是在原端口提供服务的
Nacos extended configuration
1 The dependencies are the same as above and will not be repeated here.
2 Configuration here must be configured in bootstrap.yml.
spring:
application:
name: nacos-dynamic-env
cloud:
nacos:
config:
server-addr: 192.168.0.169:8848
file-extension: properties
namespace: 459c3b94-04e8-4de2-8eaa-c55f4a8de1b1
group: DEFAULT_GROUP
extension-configs[0]:
dataId: demo
refresh: true
fileExtension: yml
group : DEFAULT_GROUP
extension-configs[1]:
dataId: user
refresh: true
fileExtension: yml
group : DEFAULT_GROUP
@RestController
@RefreshScope
public class ConfigController {
@Value("${server.port}")
private int port;
@Value("${demo.val}")
private String val;
@Value("${demo.pwd}")
private String pwd;
@RequestMapping("/info")
public String get() {
return String.format("ConfigController output, port : {%s} val : {%s} pwd : {%s}", port, val, pwd);
}
}
Final output: You can see that the configuration at the end of the same attribute will overwrite the previous one.
I highly recommend reading this blog post for a more detailed explanation.