由于eureka官方已经正式宣布:自2.0起不再维护该项目。https://github.com/Netflix/eureka
所以这里使用Consul 代码Eureka
Consule安装-单机部署:
- 注意这里使用docker 直接运行的,如果不熟悉的请阅读如下文章快速入门
Centos 7 下安装Docker
Docker 常用操作 - 使用docker 安装可以统计管理软件,不用把自己的电脑搞得亂七八糟
1.摘取镜像及安装
# 检索镜像
docker search consul
docker pull consule
# 查看镜像
docker images
# 安装
docker run --name consul -p 8500:8500 -v $PWD/data/:/consul/data/ -v$PWD/config:/consul/config -d consul
Consule安装-集群部署:
mkdir /data/docker/consul
touch docker-compose.yml
vim docker-compose.yml
加入如下内容:
version: '3.6'
services:
consul1:
image: consul:1.3.0
container_name: consul1
restart: always
network_mode: mynet
command: agent -server -client=0.0.0.0 -bootstrap-expect=3 -node=consul1
consul2:
image: consul:1.3.0
container_name: consul2
network_mode: mynet
restart: always
command: agent -server -client=0.0.0.0 -retry-join=consul1 -node=consul2
consul3:
image: consul:1.3.0
container_name: consul3
network_mode: mynet
restart: always
command: agent -server -client=0.0.0.0 -retry-join=consul1 -node=consul3
consul4:
image: consul:1.3.0
container_name: consul4
network_mode: mynet
restart: always
ports:
- 8500:8500
command: agent -client=0.0.0.0 -retry-join=consul1 -ui -node=client1
启动:
docker-compose up -d
创建maven工程
结构如下
spring-cloud-consul-parent
----provider
----consumer
parent pom.xml
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
provider相关配置
-
pom.xml文件
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-actuator
org.springframework.boot
spring-boot-configuration-processor
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.cloud
spring-cloud-starter-consul-config
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
1.18.6
-
application.yml 文件
server:
port: 9201
-
bootstrap.yml 文件
spring:
application:
name: springtest-service
cloud:
consul:
host: 192.168.199.214
port: 8500
discovery:
register: true
hostname: 192.168.199.140
serviceName: ${spring.application.name}
port: ${server.port}
healthCheckPath: /home
healthCheckInterval: 15s
tags: urlprefix-/${spring.application.name}
instanceId: ${spring.application.name}
-
SpringtestServiceApplication 文件
@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class SpringtestServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringtestServiceApplication.class, args);
}
@GetMapping("/home")
public String home() {
return "Hello World";
}
@GetMapping("/test")
public String test(@RequestParam(value = "id") Integer id) {
return "test" + id;
}
}
consumer 相关配置
-
pom.xml
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.cloud
spring-cloud-start
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
-
application.yml
server:
port: 9202
spring:
application:
name: springtest-client
cloud:
consul:
host: 192.168.199.214
port: 8500
discovery:
register: false
-
MyFeignClient
@FeignClient(name = "springtest-service", fallback = HystrixFeignFallback.class)
public interface MyFeignClient {
@GetMapping("/home")
String home();
@GetMapping("/test")
String test(@RequestParam("id") Integer id);
}
-
HystrixFeignFallback
public class HystrixFeignFallback implements MyFeignClient {
@Override
public String home() {
return "home failure";
}
@Override
public String test(Integer id) {
return "test failure "+id;
}
}
-
SpringtestClientApplication
@EnableDiscoveryClient
@RestController
@SpringBootApplication
@EnableFeignClients
public class SpringtestClientApplication {
@Autowired
private MyFeignClient myFeignClient;
public static void main(String[] args) {
SpringApplication.run(SpringtestClientApplication.class, args);
}
/**
* 调用test
*
* @param id
* @return
*/
@GetMapping("/test")
public String test(@RequestParam Integer id) {
return myFeignClient.test(id);
}
/**
* 调用home
*
* @return
*/
@GetMapping("/home")
public String home() {
return myFeignClient.home();
}
}