Spring Cloud 微服务一:Consul注册中心

2023-06-02,,

    Consul介绍  

     Consul is a service mesh solution providing a full featured control plane with service discovery, configuration, and segmentation functionality. Each of these features can be used individually as needed, or they can be used together to build a full service mesh. Consul requires a data plane and supports both a proxy and native integration model. Consul ships with a simple built-in proxy so that everything works out of the box, but also supports 3rd party proxy integrations such as Envoy. 以上是官网介绍,大概意思是:Consul是一个service mesh 解决方案,包含服务发现、配置、外部服务;这些功能可以按需单独使用。
    Consul核心能力包括:服务发现、健康检查、KV存储、安全通信、多数据中心。本章主要使用服务发现功能。Consul官网:https://www.consul.io/

    2. Consul安装有两种方式:原生安装,docker安装,本例主要采用docker方式

      docker安装:安装方法参考:https://github.com/JensenShao/consul 。以单机模式安装Consul,第一步,获取镜像:docker pull wdijkerman/consul;第二步,docker宿主机上创建文件

    {
    "data_dir": "/consul/data",
    "log_level": "INFO",
    "client_addr": "0.0.0.0",
    "ports": {
    "dns": 53
    },
    "ui": true,
    "server": true,
    "bootstrap_expect": 1,
    "disable_update_check": true
    };第三步,启动镜像:docker run -u root -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h server1 -v /root/shaozj/consul/config/my_config.json:/consul/config/my_config.json:ro  --privileged=true wdijkerman/consul;更多参数配置可参考上面链接地址。

    原生安装方式参考:https://www.consul.io/docs/install/index.html

    
    

    3.spring 集成,springcloud对Consul的基本操作进行了封装,我们可以方便地使用consul的功能,本文中主要是使用consul的服务发现功能,客户端使用Springcloud feign做http客户端以及负载均衡,以用户管理为例描述服务发现功能。

    环境准备:本机安装Maven,JDK1.8,Intellij Idea环境;spring环境:spring boot使用2.0.8.RELEASE版本,spring cloud使用Finchley.SR2, 具体版本间的兼容关系请参考spring官网:http://spring.io/projects/spring-cloud,里面有明确说明
    创建工程骨架:创建父工程parent,创建三个module:user-api,user-service,user-consumer;其中user-api中定义了服务接口,user-service和user-consumer都会依赖这个接口;user-service是对于user-api中接口的实现;user-consumer主要通过feign来调用服务
    编写user-api代码:首先,由于api工程中会用到web的一些注解,需要pom中依赖spring-boot-starter-web。创建实体类User.java, 属性有id,name,创建接口UserService:

    public interface UserService{
    @GetMapping("/all")
    List<User> getAllUsers();
    }

    这样一个接口就完成了

    接下来编写user-service,主要是对user-api接口的实现,是服务的提供方。首先,pom中需要依赖user-api,另外需要依赖spring cloud consul服务发现组件:spring-cloud-starter-consul-discovery。创建UserService实现类UserServiceImpl:

    @RestController
    public class UserServiceImpl implements UserService{
    @Override
    public List<User> getAllUsers(){
    // 模拟DAO
    List<User> userList = new ArrayList<>();
    userList.add(new User(1, "consul"));
    userList.add(new User(1, "feign")); return userList;
    }
    }

    此处还需要添加一个springboot的启动类UserServiceApplication用于启动,

    @SpringBootApplication
    @EnableDiscoveryClient
    public class UserServiceApplication{
    public static void main(String[] args){
    SpringApplication.run(UserServiceApplication.class, args);
    }
    }

    其中@EnableDiscoveryClient注解作用是让注册中心能够发现并扫描该服务。最后添加spring配置application.yml:

    spring:
    application:
    name: user-service
    cloud:
    consul:
    host: 127.0.0.1
    port: 8500
    discovery:
    register: true
    hostname: 127.0.0.1 server:
    port: 10086

    其中register表示该工程下的服务会注册到注册中心

    实现user-consumer,该工程主要是服务的调用方。同样先在pom中添加依赖包:user-api, 同时需要依赖spring cloud feign:spring-cloud-starter-openfeign, 还需要依赖:spring-cloud-starter-consul-discovery作为http客户端。首先定义一个接口UserServiceClient继承UserService(此处为feign的使用方式):

    //其中@FeignClient注解表示要调用的微服
    @FeignClient(value="user-service")
    public UserServiceClient extends UserService{}

    创建UserController调用服务:

    @RestController
    public class UserController{
    //自动注入feign客户端
    @Autowired
    private UserServiceClient userServiceClient; @GetMapping("/users")
    public List<User> getAllUsers(){
    //调用微服务
    return userServiceClient.getAllUsers();
    }
    }

    创建springboot启动类UserConsumerApplication:

    //@EnableFeignClients表示启用feign客户端,通过此注解可以使用feign
    @EnableFeignClients
    @SpringBootApplication
    public class UserConsumerApplication{
    public static void main(String[] args){
    SpringApplication.run(UserConsumerApplication.class, args);
    }
    }

    创建spring配置文件application.yml:

    spring:
    application:
    name: user-consumer
    cloud:
    consul:
    host: 127.0.0.1
    port: 8500
    discovery:
    # false表示该工程下的服务不会被注册
    register: false server:
    port: 10080

    分别启动user-service,user-consumer应用,在浏览器访问:http://localhost:10080/users,能够返回用户列表即表示成功

Spring Cloud 微服务一:Consul注册中心的相关教程结束。

《Spring Cloud 微服务一:Consul注册中心.doc》

下载本文的Word格式文档,以方便收藏与打印。