spring cloud中基于consul的服务注册与发现
文章目录
一、简介
随着netflix的eureka2.x停止开发,spring cloud服务注册与发现将不再推荐使用eureka。这是介绍spring cloud官方推荐的服务注册与发现的consul使用。
二、开发步骤
2.1 注册中心
不同于eureka中,consul不需要自己创建注册中心项目,直接下载即可,下载地址为:https://www.consul.io/downloads
这里在mac中直接使用brew安装。
#安装
brew install consul
#启动
consul agent -dev
启动后,在浏览器中输入地址:http://localhost:8500/ 即可查看注册中心界面。
2.2 服务
服务端很简单,在application.yaml中添加consul注册信息即可,如下:
spring:
application:
name: spring-cloud-provider-consul
cloud:
consul:
host: localhost
port: 8500
discovery:
serviceName: spring-cloud-provider-consul #服务名称
2.3 客户端
客户端开发步骤,如下:
-
在application.yaml中添加consul注册信息即可,如下:
spring: cloud: consul: host: localhost port: 8500
-
在启动类上添加服务发现注解@EnableDiscoveryClient
三、示例
这里以下载的注册中心、服务端项目spring_cloud_provider_consul、客户端项目spring_cloud_client_consul进行介绍。
3.1 注册中心
注册中心很简单,见前面开发步骤。
3.2 服务端
服务端项目为spring_cloud_provider_consul。
3.2.1 添加maven依赖
<?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 https://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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dragon.study</groupId>
<artifactId>spring_cloud_provider_consul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_cloud_provider_consul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2.2 添加application.yaml配置
server:
port: 8067
spring:
application:
name: spring-cloud-provider-consul
cloud:
consul:
host: localhost
port: 8500
discovery:
serviceName: spring-cloud-provider-consul #服务名称
management:
endpoints:
web:
exposure:
include: "*"
3.2.3 服务相关类
定义bean类:
package com.dragon.study.spring_cloud_provider_consul.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Stu {
private String stuId;
private Integer age;
}
定义服务controller:
package com.dragon.study.spring_cloud_provider_consul.controller;
import com.dragon.study.spring_cloud_provider_consul.bean.Stu;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("stu")
public class StuController {
@RequestMapping("getStu")
private Object getStu(Stu stu) {
System.out.println(stu);
return stu;
}
}
定义启动类:
package com.dragon.study.spring_cloud_provider_consul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringCloudProviderConsulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudProviderConsulApplication.class, args);
}
}
3.3 客户端
客户端项目为spring_cloud_client_consul。
3.3.1 添加maven依赖
<?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 https://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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dragon.study</groupId>
<artifactId>spring_cloud_provider_consul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_cloud_provider_consul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.3.2 添加application.yaml配置
server:
port: 8056
spring:
application:
name: spring-cloud-client-consul
cloud:
consul:
host: localhost
port: 8500
feign:
hystrix:
enabled: true #feign添加支持熔断处理
3.3.3 添加客户端相关类
定义bean类:
package com.dragon.study.spring_cloud_client_consul.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Stu {
private String stuId;
private Integer age;
}
添加服务调用类:
package com.dragon.study.spring_cloud_client_consul.service;
import com.dragon.study.spring_cloud_client_consul.bean.Stu;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
//指定调用服务为spring-cloud-provider-consul,熔断服务为StuServiceHystrix.class
@FeignClient(value = "spring-cloud-provider-consul", fallback = StuServiceHystrix.class)
public interface StuServiceFeign {
@RequestMapping("/stu/getStu")
Stu getStu(@RequestParam String stuId, @RequestParam Integer age);
}
添加服务熔断类:
package com.dragon.study.spring_cloud_client_consul.service;
import com.dragon.study.spring_cloud_client_consul.bean.Stu;
import org.springframework.stereotype.Service;
@Service
public class StuServiceHystrix implements StuServiceFeign {
//远程服务不可用时调用此实现
@Override
public Stu getStu(String stuId, Integer age) {
return new Stu("000",18);
}
}
添加调用controller:
package com.dragon.study.spring_cloud_client_consul.controller;
import com.dragon.study.spring_cloud_client_consul.bean.Stu;
import com.dragon.study.spring_cloud_client_consul.service.StuServiceFeign;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("stu")
public class StuController {
@Resource
private StuServiceFeign stuServiceFeign;
@RequestMapping("getStu")
public Object getStu(Stu stu) {
return stuServiceFeign.getStu(stu.getStuId(), stu.getAge());
}
}
添加启动类:
package com.dragon.study.spring_cloud_client_consul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudClientConsulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudClientConsulApplication.class, args);
}
}
3.4 测试
依次启动注册中心、服务端项目spring_cloud_provider_consul、客户端项目spring_cloud_client_consul。
测试:
GET http://localhost:8056/stu/getStu?stuId=001&age=17
Accept: application/json
响应:
{
"stuId": "001",
"age": 17
}