一、Windows安装Consul
- 下载Consul:https://www.consul.io/
- 解压缩
- 启动consul
consul agent -dev
#dev表示开发模式运行,server表示以服务器模式运行
说明:Server模式推荐使用3-5台服务器,参考后续更新的基于Linux部署“服务中心”集群方式。
如果单在Windows下运行Server模式则会报以下错误。
常用命令:
- consul members
- consul members -detailed
- consul monitor
- 查看Web管理界面:http://localhost:8500
二、用consul实现服务提供者Provider
- 新建项目
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
-
开启“服务注册”功能
启动类中添加注解:@EnableDiscoveryClient,如下图。
-
实现服务接口
新建controller目录,添加以下代码
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${provider.name}")
private String name;
@Value("${server.port}")
private String port;
@RequestMapping("/hello")
public String sayHello(){
return "Provider=" + name + "; Port=" + port;
}
}
- 配置“服务器提供者”集群
新建两个properties:
application-consul-provider1.properties
spring.application.name=consul-provider
server.port=8504
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=service-provider
provider.name=p1
application-consul-provider2.properties
spring.application.name=consul-provider
server.port=8505
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=service-provider
provider.name=p2
- 通过Maven打包后进入Target目录
- 启动服务集群
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=consul-provider1
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=consul-provider2
- 查看服务,在浏览器中输入以下地址进行确认结果
http://localhost:8504/hello
结果显示为Provider=p1; Port=8504
http://localhost:8505/hello
结果显示为Provider=p2; Port=8505
此时Consul服务中心管理台多了一组Service:service-provider
三、实现服务消费者
- 创建服务消费者项目,同服务提供者创建方式,项目名称取名为:Consul Consumer
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 添加配置
spring.application.name=consul-consumer
server.port=8506
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.register=false
- 编写服务调用接口
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
@RestController
public class HelloController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@RequestMapping("/hello")
public String sayHello(){
ServiceInstance serviceInstance = loadBalancerClient.choose("service-provider");
URI uri = serviceInstance.getUri();
String callService = new RestTemplate().getForObject(uri + "/hello", String.class);
return callService;
}
}
- 打包并启动Consumer服务
java -jar demo-0.0.1-SNAPSHOT.jar
- 检查消费结果
http://localhost:8506/hello
刷新页面会轮流出现
Provider=p1; Port=8504
Provider=p2; Port=8505