2020最新版Springcloud-Alibaba Consul篇(四)
一. 安装Consul(Windos版本)
官网下载太慢了,我上传到百度云了
百度云链接
提取码: fs72
1.1 下载并解压到文件夹(省略)
1.2 在路径下输入cmd进入命令行(省略)
1.3 在命令行输入consul,如果出现下面一堆东西,则安装成功
1.4 输入 consul agent -dev 启动 Consul
在我们本地8500端口可以看见Consul自带的UI界面
二. 新建支付服务module cloud-providerconsul-payment8006
2.1 模块8006结构(省略)
2.2 改pom
<dependencies> <!--springcloud consul-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <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> <!--日常通用jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.3 建 application.yml 配置文件
server: port: 8006 spring: application: name: consul-provider-payment ###consul注册中心地址 cloud: consul: host: localhost port: 8500 discovery: #hostname: 127.0.0.1 service-name: ${spring.application.name}
2.5 业务类Controller层
/** * @Author: cws * @Date: 17:33 2020/8/13 * @Description: * @Version v1.0 */ @RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/consul") public String paymentConsul(){ return "Spring cloud with consul port: "+serverPort+"\t "+ UUID.randomUUID().toString(); } }
2.6 测试
打开 consul UI界面可以看到我们的微服务已经注册进来
在conusl里面可以看到其他的一些数据
测试业务
三. 新建modul cloud-consumerconsul-order80
3.1 目录结构(省略)
3.2 改pom
<dependencies> <!--springcloud consul-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--springboot整合web组件--> <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> <!--日常通用jar包配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
3.3 建 application.yml 配置文件
### consul服务端口号 server: port: 80 spring: application: name: cloud-consumer-order ###consul服务注册中心 cloud: consul: host: localhost port: 8500 discovery: #hostname: 127.0.0.1 service-name: ${spring.application.name}
3.4 主启动类
/** * @Author: cws * @Date: 18:00 2020/8/13 * @Description: * @Version v1.0 */ @SpringBootApplication @EnableDiscoveryClient public class OrderControllerMain80 { public static void main(String[] args) { SpringApplication.run(OrderControllerMain80.class, args); }
3.5 业务类Controller层
/** * @Author: cws * @Date: 18:58 2020/8/13 * @Description: * @Version v1.0 */ @RestController @Slf4j public class OrderConsulContext { public static final String INVOKE_URL ="http://consul-provider-payment"; @Resource private RestTemplate restTemplate; @GetMapping(value = "/consumer/payment/consul") public String getPaymentInfo(){ return restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class); } }
3.6 配置Bean RestTemplate模板
/** * @Author: cws * @Date: 18:54 2020/8/13 * @Description: * @Version v1.0 */ @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
3.7 测试启动
业务类测试