注意:springcloud-consul服务注册到集群中(agent client)任意一个节点,就会同步服务到集群中的所有节点,即是springcloud中host配置的是(agent client)节点的ip,(agent client)上所注册的服务会自动同步到其他的(consul server)。
注册中心搭建请参照:Consul注册中心高可用集群搭建(Consul HA)
项目结构如下:
服务提供者相关配置
------pom.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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.1.2.RELEASE</version>-->
<!--<version>2.1.1.RELEASE</version>-->
<!--<version> 2.1.3.RELEASE</version>-->
<!--<version>2.1.0.RELEASE</version>-->
<version>2.0.9.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.springcloud.consul.register</groupId>
<artifactId>SSO-SpringCloud-Consul</artifactId>
<version>1.0-SNAPSHOT</version>
<name>SSO-SpringCloud-Consul</name>
<!-- 设定主仓库,按设定顺序进行查找。 -->
<repositories>
<repository>
<id>repos</id>
<name>Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!-- 设定插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>repos</id>
<name>Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</pluginRepository>
</pluginRepositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</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-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
==============================================================
application.properties
spring.profiles.active=dev
application-dev.properties
spring.application.name=spring-cloud-consul-provider
## 配置服务端口
server.port=8855
# 表示注册时使用IP而不是hostname
spring.cloud.consul.discovery.prefer-ip-address=true
#consul~IP,该ip为agent client注册中心的IP
spring.cloud.consul.host=192.168.227.140
# 表示启用注册
spring.cloud.consul.discovery.register=true
# 表示启用发现
spring.cloud.consul.discovery.enabled=true
#定义 consul 健康检查路径
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.tags = url-prefix-/${spring.application.name}
# # 执行监控检查的频率
spring.cloud.consul.discovery.health-check-interval=10s
spring.cloud.consul.discovery.health-check-critical-timeout=30s
# # 服务注册标识,格式为:应用名称+服务器IP+端口,避免注册两个相同的实例,只能看到一个的情况
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
#consul~端口
spring.cloud.consul.port=8500
# springcloud 向consul注册服务时,显示Unhealthy Nodes(不健康的节点) 注册有一个小红叉
spring.cloud.consul.discovery.heartbeat.enabled=true
# 监控访问地址
# http://localhost:8855/actuator/health
management.endpoint.health.show-details=always
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=consul-service-producer
=================controller==========
package com.springcloud.consul.register.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@RestController
public class ConsulProviderController {
@Value("${server.port}")
private int port;
@Value("${spring.application.name}")
private String applicationName;
@GetMapping("/list")
public List<ConsulUser> findZkUser(){
List<ConsulUser> resultList = new ArrayList<ConsulUser>();
resultList.add(new ConsulUser("consul-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
resultList.add(new ConsulUser("consul-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
resultList.add(new ConsulUser("consul-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
resultList.add(new ConsulUser("consul-server-"+applicationName+"-"+new Random().nextInt(10000) ,new Random().nextInt(120),port));
return resultList;
}
}
===================实体类======
package com.springcloud.consul.register.controller;
import java.io.Serializable;
public class ConsulUser implements Serializable {
private String name;
private int age;
private int port;
public ConsulUser(String name, int age, int port) {
this.name = name;
this.age = age;
this.port = port;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Override
public String toString() {
return "Zkuser{" +
"name='" + name + '\'' +
", age=" + age +
",server port=" + port +
'}';
}
}
===================启动类========================
package com.springcloud.consul.register;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* ConsulProviderApplication服务提供者
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProviderApplication {
public static void main( String[] args ) {
SpringApplication.run(ConsulProviderApplication.class,args);
}
}
====================服务消费者====================
----------------pom.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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.springcloud.consul.register.consumer</groupId>
<artifactId>SSO-SpringCloud-Consul-Consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 设定主仓库,按设定顺序进行查找。 -->
<repositories>
<repository>
<id>repos</id>
<name>Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!-- 设定插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>repos</id>
<name>Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</pluginRepository>
</pluginRepositories>
<name>SSO-SpringCloud-Consul-Consumer</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<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-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
=====================================
application.properties
spring.profiles.active=dev
application-dev.properties
spring.application.name=spring-cloud-consul-consumer
## 配置服务端口
server.port=8866
# 表示注册时使用IP而不是hostname
spring.cloud.consul.discovery.prefer-ip-address=true
#consul~IP,该IP为Consul-client注册中心的IP,会自动复制注册的服务到其他consol-server节点
spring.cloud.consul.host=192.168.227.140
# 表示启用注册
spring.cloud.consul.discovery.register=true
# 表示启用发现
spring.cloud.consul.discovery.enabled=true
#定义 consul 健康检查路径
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.tags = url-prefix-/${spring.application.name}
# # 执行监控检查的频率
spring.cloud.consul.discovery.health-check-interval=10s
spring.cloud.consul.discovery.health-check-critical-timeout=30s
# # 服务注册标识,格式为:应用名称+服务器IP+端口,避免注册两个相同的实例,只能看到一个的情况
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
#consul~端口
spring.cloud.consul.port=8500
# springcloud 向consul注册服务时,显示Unhealthy Nodes(不健康的节点) 注册有一个小红叉
spring.cloud.consul.discovery.heartbeat.enabled=true
# 监控访问地址
# http://localhost:8855/actuator/health
management.endpoint.health.show-details=always
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=consul-service-consumer
================================service===============================================
package com.springcloud.consul.register.consumer.service;
import com.springcloud.consul.register.consumer.domain.ConsulUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Service
@FeignClient("consul-service-producer") // 該值為服務提供者的名稱
public interface ConsulUserService {
@GetMapping("/list")
List<ConsulUser> getList();
}
=====================domain==========================
package com.springcloud.consul.register.consumer.domain;
import java.io.Serializable;
public class ConsulUser implements Serializable {
private String name;
private int age;
private int port;
public ConsulUser() {
}
public ConsulUser(String name, int age, int port) {
this.name = name;
this.age = age;
this.port = port;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Override
public String toString() {
return "ConsulUser{" +
"name='" + name + '\'' +
", age=" + age +
",server port=" + port +
'}';
}
}
=======================controller=======================
package com.springcloud.consul.register.consumer.controller;
import com.springcloud.consul.register.consumer.domain.ConsulUser;
import com.springcloud.consul.register.consumer.service.ConsulUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ConsulUserController {
@Autowired
private ConsulUserService consulUserService;
@GetMapping("/list")
public List<ConsulUser> getList() {
return consulUserService.getList();
}
}
=====================启动类========================
package com.springcloud.consul.register.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsulConsumerApplication {
public static void main( String[] args ) {
SpringApplication.run(ConsulConsumerApplication.class,args);
}
}
================分别启动服务提供端与服务消费端====查看任意一个节点都能看到所注册的服务========
可以看到注册成功
访问服务消费端:http://localhost:8866/list
[
{"name":"consul-server-spring-cloud-consul-provider-6045","age":2,"port":8855},
{"name":"consul-server-spring-cloud-consul-provider-9988","age":117,"port":8855},
{"name":"consul-server-spring-cloud-consul-provider-1198","age":33,"port":8855},
{"name":"consul-server-spring-cloud-consul-provider-5756","age":78,"port":8855}
]
至此SpringCloud与Consul整合成功!................