Registration services to the service center (Consul)

Registration services to the service center (Consul)

Add rely POM file

Add the following dependence in POM file:

                <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>
            <version>2.1.3.RELEASE</version>
        </dependency>

spring-boot-starter-actuator responsible for health checks, spring-cloud-starter-consul -discovery responsible for the support of the Consul.
The version number must be clearly referenced in the spring-cloud-starter-consul- discovery, BOOT version of our project is 2.1.8.RELEASE, spring-cloud-starter- consul-discovery version number is 2.1.3.RELEASE, can Spring Cloud's official website found.
Alt text

DependencyManagement manage or use the version number, add the following in the POM file can not indicate the version number of spring-cloud-starter-consul-discovery of:

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <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>
    
    <dependencies>

        .....

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        .....

    </dependencies>

Profiles

spring.application.name=spring-cloud-provider-01
server.port=9000
spring.cloud.consul.host=localhost
#consul端口可以自行修改
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=service-provider

Startup class

SpringCloudProviderApplication.java

@SpringBootApplication
//支持服务发现
@EnableDiscoveryClient
public class SpringCloudProviderApplication {

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

}

Start Service

Automatically start the service after the completion of the process of registration service, return to consul control interface can see the good service has been registered:
Alt text
Click service-provider can see that the service is only a micro service:
Alt text

Load Balancing

We refer spring-cloud-provider program in a micro-replication services spring-cloud-provider-second, and HelloWorld class is modified to distinguish:
spring-cloud-provider project HelloWorld class:

@RestController
public class HelloWorld {

    @RequestMapping("/hello")
    public String Hello(@RequestParam String name){
        return "你好!" + name + ",这是第一个微服务。";
    }
}

spring-cloud-provider-second project HelloWorld class:

@RestController
public class HelloWorld {

    @RequestMapping("/hello")
    public String Hello(@RequestParam String name){
        return "你好!" + name + ",这是第二个微服务。";
    }
}

Modify spring-cloud-provider-second project of the port number:
spring-cloud-provider-second project application.properties contents of the file:

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

Start spring-cloud-provider-second project, view the service-provider service providers have emerged two service providers:
Alt text

Source

Github repository: https: //github.com/sunweisheng/spring-cloud-example

Guess you like

Origin www.cnblogs.com/bluersw/p/11610699.html