Spring 体系图
版本关系
版本对应关系wiki
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:8761eureka:instance:hostname: localhost
client:registerWithEureka:falsefetchRegistry:falseserviceUrl: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@LoadBalancedpublicRestTemplaterestTemplate(){returnnewRestTemplate();}
5.2 真实调用
@GetMapping("/hello/{id}")publicStringhello(@PathVariableString id){return restTemplate.getForObject("http://EUREKA-PROVIDER/hello/"+id,String.class);}
注意:
@LoadBalanced这个注解一定要加上(org.springframework.cloud.client.loadbalancer)
集群模式:
https://cloud.tencent.com/developer/article/1730733
Naco 实践
1 首先我们需要安装 Nacos Server
可以选用Docker ,但是一定得注意哈这里会有坑,需要暴露三个端口:
docker run -d-p8848:8848 -p9848:9848 -p9849:9849 --name nacos -e"MODE=standalone"-eNACOS_AUTH_IDENTITY_KEY=serverIdentity -eNACOS_AUTH_IDENTITY_VALUE=security -eNACOS_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:8088spring: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:8089application: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刷新属性的能力publicclassConfigController{@Value("${server.port}")privateint port;@RequestMapping("/info")publicStringget(){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:8848file-extension: properties
namespace: 459c3b94-04e8-4de2-8eaa-c55f4a8de1b1
group: DEFAULT_GROUP
extension-configs[0]:dataId: demo
refresh:truefileExtension: yml
group: DEFAULT_GROUP
extension-configs[1]:dataId: user
refresh:truefileExtension: yml
group: DEFAULT_GROUP
@RestController@RefreshScopepublicclassConfigController{@Value("${server.port}")privateint port;@Value("${demo.val}")privateString val;@Value("${demo.pwd}")privateString pwd;@RequestMapping("/info")publicStringget(){returnString.format("ConfigController output, port : {%s} val : {%s} pwd : {%s}", port, val, pwd);}}
最后输出:可以看到相同的属性后面的配置会覆盖前面。
强烈推荐看一下这篇博文,做了更详细的讲解
版权归原作者 njnu@liyong 所有, 如有侵权,请联系我们删除。