1. 安装consul
自行搜索
快速启动的方法:
./consul agent -dev -client=0.0.0.0
--client 用0.0.0.0表示不限客户端(调用方)的IP
注意: OS X 用户:Consul 使用 hostname 做 node name,如果 node name 包含句点,会导致 DNS 无法工作,所以需要显式设置 node name 用 -node flag
./consul agent -dev -client=0.0.0.0 -node=node0
我的开发地址是:192.168.6.244, 访问http://192.168.6.244:8500可以看到界面:
2.创建Spring-Cloud服务,注册到上面的地址
pom.xml 主要内容(spring-boot-starter-web/test自己添加):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
main.class
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulCloudApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulCloudApplication.class, args);
}
@GetMapping(value = "/hello")
public String hello() {
return "My First Consul.";
}
}
application.yml:
spring:
application:
name: consul-cloud-1
cloud:
consul:
host: 192.168.6.244
port: 8500
discovery:
service-name: my-first-consul-service # 服务调用的名字
health-check-interval: 15s
server:
port: 8501
同时复制一份副本,修改端口为8502,application name 为consul-cloud-2, hello()方法返回为"My Fitst Consul 2"
启动这两个应用之后,再访问http://192.168.6.244:8500
你会看到:
这说明已经注册成功
3.开启spring-cloud-comsumer来消费服务
pom.xml主要内容
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
目录结构:
application.yml:
spring:
application:
name: consul-cloud-consumer
cloud:
consul:
host: 192.168.6.244
port: 8500
discovery:
register: false # 仅作为消费者, 不注册服务
server:
port: 8503
ConsulCloudConsumerApplication.class:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsulCloudConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulCloudConsumerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
com.springcloud.controller下的Consumer:
@RestController
public class Consumer {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private RestTemplate restTemplate;
@Autowired
private FeignService feignService;
@GetMapping(value = "/getServices")
public Object getServices() {
return discoveryClient.getInstances("my-first-consul-service");
}
// 轮询的选择同服务(来自不同的客户注册中心,IP不同)
@GetMapping(value = "/chooseService")
public Object chooseService() {
return loadBalancerClient.choose("my-first-consul-service").getUri().toString();
}
// -------------------------- ribbon --------------------------
@GetMapping(value = "/ribbon-call")
public String ribbonCall() {
String method = "hello";
return restTemplate.getForEntity("http://my-first-consul-service/" + method, String.class).getBody();
}
// -------------------------- Feign --------------------------
@GetMapping(value = "/feign-call")
public String feignCall() {
return feignService.hello();
}
}
com.springcloud.service.FeignService下的FeignService
@FeignClient("my-first-consul-service")
@Component
public interface FeignService {
@RequestMapping(value = "/hello")
public String hello(); // 如果有参数, 必须用RequestParam注解
}
启动消费者项目,分别访问:
http://localhost:8503/getServices
可以看到:
[
{
"serviceId": "my-first-consul-service",
"host": "192.168.101.137",
"port": 8501,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://192.168.101.137:8501",
"scheme": null
},
{
"serviceId": "my-first-consul-service",
"host": "192.168.101.137",
"port": 8502,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://192.168.101.137:8502",
"scheme": null
}
]
访问:http://localhost:8503/ribbon-call和http://localhost:8503/feign-call
轮回出现:
My First Consul.
My First Consul. 2
参考资料 :
http://www.ityouknow.com/springcloud/2018/07/20/spring-cloud-consul.html