Spring 体系图
版本关系
eureka 实践
1 父工程依赖
<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 搭建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 搭建服务提供者
<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 搭建消费者
<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 服务消费方
5.1 使用RestTemplate 进行RPC调用
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
5.2 真实调用
@GetMapping("/hello/{id}")
public String hello(@PathVariable String id){
return restTemplate.getForObject("http://EUREKA-PROVIDER/hello/"+id, String.class);
}
注意:
@LoadBalanced这个注解一定要加上(org.springframework.cloud.client.loadbalancer)
扫描二维码关注公众号,回复:
17320881 查看本文章
![](/qrcode.jpg)
集群模式:
https://cloud.tencent.com/developer/article/1730733
Naco 实践
1 首先我们需要安装 Nacos Server
可以选用Docker ,但是一定得注意哈这里会有坑,需要暴露三个端口:
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 父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 服务提供者
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 服务消费者
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 调用方式和上面的一样,使用负载均衡的RestTemplate 进行调用
Nacos 配置管理
1 父工程依赖
<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 具体模块,引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
3 配置bootstrap.yml
写在上面application.yml在较高的版本据说也可以,但是我没有成功,后面测试一下来补充吧。
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
命名規則:
配置列表:
4 验证 你可以在nacos里面修改端口,然然后启动,发现你配置的端口会生效:
@RestController
@RefreshScope 、// 这个注解别忘了这个提供了bean刷新属性的能力
public class ConfigController {
@Value("${server.port}")
private int port;
@RequestMapping("/info")
public String get() {
return "NacosDynamicPortController output, port: " + port;
}
}
修改不同的端口发现info会打印不同的端口,但是特别注意哈我们一般不用来更新端口,因为这个端口改了,但是服务还是在原端口提供服务的
Nacos 拓展配置
1 依赖 和上面一样的 这里不在赘述。
2 配置这里是必须配置在 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);
}
}
最后输出:可以看到相同的属性后面的配置会覆盖前面。