1.5 创建服务消费者
spring-cloud-alibaba-demo-consumer
pom.xml
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>com.lzj</groupId> <artifactId>spring-cloud-alibaba-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>spring-cloud-alibaba-demo-consumer</artifactId> <packaging>jar</packaging> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.lzj.spring.cloud.alibaba.consumer.ConsumerApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
ConsumerApplication
java
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } }
application.yml
yaml
spring: application: name: service-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848 server: port: 8081 #健康检查 management: endpoints: web: exposure: include: "*"
feignClient
java
@FeignClient(value = "service-provider") public interface ConsumerService { @GetMapping(value = "/echo/{string}") String echo(@PathVariable("string") String string); }
controller
java
@RestController public class ConsumerController { @Resource private ConsumerService consumerService; @GetMapping("/echo/{str}") public String echo(@PathVariable String str){ return "Consumer send str and reply is:" + consumerService.echo(str); } }
1.6 加入分布式配置中心
- 在消费者中增加依赖
xml
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
- 创建
bootstrap.properties
文件
properties
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
- 将
application.yml
中的配置 添加再nacos中 - 将
application.yml
文件删除,这里为保证项目完整,更名为tmp_application.yml
- 启动测试
- 动态刷新配置
在nacos配置中加入新配置
yaml
user: name: 张三
更改ConsumerController
增加注解@RefreshScope
java
@RestController @RefreshScope public class ConsumerController { @Value("${user.name}") private String username; @GetMapping("/user") public String user(){ return username; } }
- 启动测试 访问
http://localhost:8081/user
返回结果为:张三 - 更改配置为
yaml
user: name: 李四
- 再次访问: 可以看到结果为:李四
1.7 加入sentinel
流量监控
获取控制台
从 release 页面 下载最新版本的控制台 jar 包。
启动控制台 sentinel-dashboard-version.jar
替换成真实jar包名
shell
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-version.jar
引入依赖
xml
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
当前
spring-cloud-starter-alibaba-sentinel
并不支持最新版sentinel
1.7.1的客户端,所以我们下载1.7.0的客户端
- 配置控制台信息
yml
spring: cloud: sentinel: transport: port: 8719 dashboard: localhost:8080
- 配置文件打开 Sentinel 对 Feign 的支持:
feign.sentinel.enabled=true
- 加入
spring-cloud-starter-openfeign
依赖使 Sentinel starter 中的自动化配置类生效(已加入)
2.使用dubbo
1.1 创建dubbo服务提供者
spring-cloud-alibaba-demo-dubbo-provider
pom.xml
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>com.lzj</groupId> <artifactId>spring-cloud-alibaba-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-alibaba-demo-dubbo-provider</artifactId> <packaging>pom</packaging> <properties> <nacos.version>1.1.3</nacos.version> <dubbo.version>2.7.3</dubbo.version> <spring-context-support.version>1.0.6</spring-context-support.version> </properties> <dependencies> <!-- Apache Dubbo Begin --> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-serialization-kryo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-common</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.spring</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-context-support.version}</version> </dependency> <!-- Apache Dubbo End --> </dependencies> <modules> <module>spring-cloud-alibaba-demo-dubbo-provider-api</module> <module>spring-cloud-alibaba-demo-dubbo-provider-service</module> </modules> </project>
1.2 创建api
spring-cloud-alibaba-demo-dubbo-provider-api
pom.xml
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>com.lzj</groupId> <artifactId>spring-cloud-alibaba-demo-dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-alibaba-demo-dubbo-provider-api</artifactId> <packaging>jar</packaging> </project>
interface
java
package com.lzj.spring.cloud.alibaba.dubbo.provider; /** * @author Zijian Liao * @date 2020/1/17 18:36 * @description */ public interface HelloService { String hello(String string); }