SpringCloud integrated consul

1: Create a new project:

The next step has been to,

porm.xml add dependencies

 <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </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>Finchley.SR2</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>

springboot version I am using 2.0.5, 2.1.6 do not know why the use of invalid

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 

 

 

2: Start class adds @EnableDiscoveryClient comment expressed support for service discovery

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulDemo2Application {

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

}

3: Profile application.properties (Consul's address and port number default is localhost:. 8500, if not this address can configure itself spring.cloud.consul.discovery.serviceName refers to the Consul registered name of the service, the client will be late according to this name to a service call.)

spring.application.name=spring-cloud-consul-producer
server.port=8501
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=service-producer

4: Create a new HelloController, hello provide services

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello,consul";
    }
}

To simulate the load balancing register copy above items rename spring-cloud-consul-producer2, modify the corresponding port 8507, change the hello method return value: "hello consul 2", and modified to start up two complete project.

Consul consumer side: spring-cloud-consul-consumer
to create a spring-cloud-consul-consumer items, pom file and the above examples consistent.
Profiles (clients can register to set Consul, the Consul may not be registered with the registry, to choose according to our business, need only obtain service information can be provided through the interface Consul outside while using the service.)

application.properties

Spring-Cloud-= spring.application.name consul- Consumer 
the server.port = 8503 
spring.cloud.consul.host = 127.0.0.1 
spring.cloud.consul.port = 8500 
# setting does not need to register the consul 
spring.cloud. consul.discovery.register = false

 

Start class does not need @EnableDiscoveryClient

@SpringBootApplication
public class ConsulConsumerApplication {

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

}

测试,创建一个 ServiceController ,试试如果去获取 Consul 中的服务。

@RestController
public class ServiceController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 获取所有的服务
     * */

    @RequestMapping("/services")
    public Object services(){
        return discoveryClient.getInstances("service-producer");

    }

    /**
     * 从所有服务中选择一个服务(轮询)
     */
    @RequestMapping("/discover")
    public Object discover() {
        return loadBalancerClient.choose("service-producer").getUri().toString();
    }


}

运行services方法:

访问 http://localhost:8503/services

 源码:https://gitee.com/Hiro-D/Java/tree/master/consul-demo

 

参考文章:https://blog.csdn.net/qq_26641781/article/details/82080659

Guess you like

Origin www.cnblogs.com/a565810497/p/11078307.html