栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud -- consul

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud -- consul

SpringCloud 首先要解决的就是服务注册。

服务注册用例图:

上图分析:

三个角色,服务方,业务提供方,业务消费方

服务方:开启注册服务,暴露服务注册地址和端口

业务提供方:按照服务方提供的ip地址、端口进行服务注册(可以是多个提供方,业务功能一样,这是为了防止单体宕机,使用这个集群,多个提供方他们注册的业务名都一样,具体调用是通过轮询算法进行调用,见下面代码注解的 @LoadBalanced)

业务消费者:按照服务方提供的ip地址、端口进行服务注册,通过提供方在业务注册的注册名进行远程调用(具体调用代码实例使用RestTemplate)

代码:

1. 提供方:6001

导入相对应的 pom 依赖:



    
        springcloud
        com.lidantao
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consul-provider-6001

    
        8
        8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.projectlombok
            lombok
        

        
            org.springframework.cloud
            spring-cloud-starter-consul-discovery
        
    

yml 配置文件

server:
  port: 6001
spring:
  application:
    name: consul-provider
  cloud:
    consul:
      # 注册的地址
      host: localhost
      # 注册的端口
      port: 8500
      discovery:
        # 对外暴露的服务名称
        service-name: ${spring.application.name}

启动类

package com.lidantao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


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

controller

package com.lidantao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    @Value("${server.port}")
    private String consul_port;

    @RequestMapping("/provider/getTest")
    public String getTest(){
        return "consul : " + consul_port;
    }


}

2. 提供方:6002

导入相对应的 pom 依赖:



    
        springcloud
        com.lidantao
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consul-provider-6002

    
        8
        8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.projectlombok
            lombok
        

        
            org.springframework.cloud
            spring-cloud-starter-consul-discovery
        
    

yml 配置文件

server:
  port: 6002
spring:
  application:
    name: consul-provider
  cloud:
    consul:
      # 注册的地址
      host: localhost
      # 注册的端口
      port: 8500
      discovery:
        # 对外暴露的服务名称
        service-name: ${spring.application.name}

启动类

package com.lidantao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


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

controller

package com.lidantao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    @Value("${server.port}")
    private String consul_port;

    @RequestMapping("/provider/getTest")
    public String getTest(){
        return "consul : " + consul_port;
    }


}

 3. 消费方:60

导入相对应的 pom 依赖:



    
        springcloud
        com.lidantao
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consul-consumer-60

    
        8
        8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.projectlombok
            lombok
        

        
            org.springframework.cloud
            spring-cloud-starter-consul-discovery
        
    

yml 配置文件

server:
  port: 60
spring:
  application:
    name: consul-consumer
  cloud:
    consul:
      # 注册的地址
      host: localhost
      # 注册的端口
      port: 8500
      discovery:
        # 对外暴露的服务名称
        service-name: ${spring.application.name}

启动类

package com.lidantao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


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

controller

package com.lidantao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class TestController {


    @Autowired
    private RestTemplate restTemplate;

    private String PROVIDER_URL = "http://consul-provider/provider/getTest";

    @RequestMapping("/consumer/getTest")
    public String getTest(){
        return restTemplate.getForObject(PROVIDER_URL, String.class);
    }


}

configuration

package com.lidantao.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
public class MyConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

4. consul 注册页面

5. 服务请求页面

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/716252.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号