SpringCloud 使用consul作为微服务注册中心

eureka宣布闭源,使用consul作为服务注册中心。

1、parent pom文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yyh</groupId>
    <artifactId>springcloudlearn</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modules>
        <module>consulservice1</module>
        <module>consulservice2</module>
        <module>consulservice3</module>
        <module>consulclient1</module>
        <module>consulclient3</module>
        <module>consulclient4</module>
        <module>consulservice3interface</module>
    </modules>



    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、微服务接口pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloudlearn</artifactId>
        <groupId>com.yyh</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consulservice3interface</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.3</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </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>
</project>

3、微服务接口实体类

package com.yyh.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {

    private int id;
    private String firstName;
    private String lastName;

    public Customer() {
    }

    public Customer(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

4、微服务接口 HelloService接口

package com.yyh.entity;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

public interface HelloService {

    @RequestMapping(value = "say")
    String say();

    @RequestMapping(value = "s/{id}")
    Customer s(@PathVariable("id") String id);
}

5、微服务提供者pom,在pom中依赖上一个接口

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloudlearn</artifactId>
        <groupId>com.yyh</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consulservice3</artifactId>


    <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>
    <dependencies>
         <!-- 注意 依赖接口-->
        <dependency>
            <groupId>com.yyh</groupId>
            <version>1.0-SNAPSHOT</version>
            <artifactId>consulservice3interface</artifactId>
        </dependency>

        <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.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

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

        <!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

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

        <!--服务发现依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--用于consul配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

6、微服务提供者application.properties

spring.application.name=consulservice03
server.port=8003
spring.cloud.consul.discovery.instance-id=consulservice03
# 微服务健康监测url
spring.cloud.consul.discovery.health-check-path= /actuator/health
# consul注册中心地址
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500

7、微服务提供者具体实现HelloService接口 

package com.yyh.springcloudlearn.consulservice3.controller;

import com.yyh.entity.Customer;
import com.yyh.entity.HelloService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/12/21 0021.
 */
@RestController
@RequestMapping("hello")
public class HelloController implements HelloService {
    @Value("${spring.application.name}")
    private String serviceName;

    @Value("${server.port}")
    private int servicePort;
    @Override
    public String say() {
        return "hello world! I'm '" + serviceName + ":" + servicePort + "'";
    }

    @Override
    public Customer s(@PathVariable("id") String id) {
        Customer customer = new Customer();
        customer.setLastName(id);
        return customer;
    }
}

8、微服务提供者启动类

package com.yyh.springcloudlearn;

import com.yyh.entity.Customer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulService3App  {

    @Value("${spring.application.name}")
    private String serviceName;

    @Value("${server.port}")
    private int servicePort;

    @RequestMapping("/say")
    public String say(){
        return "hello world! I'm '" + serviceName + ":" + servicePort + "'";
    }
    @RequestMapping("/s/{id}")
    public Customer s(@PathVariable("id") String id) {
        Customer customer = new Customer();
        customer.setLastName(id);
        return customer;
    }


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

}

9、微服务消费者pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloudlearn</artifactId>
        <groupId>com.yyh</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consulclient4</artifactId>

    <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>
    <dependencies>
        <!--注意 依赖接口 -->
        <dependency>
            <groupId>com.yyh</groupId>
            <version>1.0-SNAPSHOT</version>
            <artifactId>consulservice3interface</artifactId>
        </dependency>

        <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.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

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

        <!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

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

        <!--服务发现依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--用于consul配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>

        <!--Feign的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!--断路器依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--断路器面板-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

    </dependencies>

</project>

10、微服务消费者application.properties

spring.application.name=consulclient04
server.port=8014
feign.hystrix.enabled=true
# 微服务健康监测url
spring.cloud.consul.discovery.health-check-path= /actuator/health
# consul注册中心地址
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#SpringCloud-Feign 第一次请求timeout问题
#索性禁用Feign的Hystrix
#hystrix.metrics.enabled=false
#SpringCloud-Feign 第一次请求timeout问题
feign.httpclient.connection-timeout=10000

11、微服务消费者FeignClient

package com.yyh.service;

import com.yyh.entity.HelloService;
import org.springframework.cloud.openfeign.FeignClient;

/**
 * Created by Administrator on 2018/12/21 0021.
 */
@FeignClient(value = "consulservice03",fallback = FallbackHelloService.class)
public interface HelloServiceConsumer extends HelloService {
}

12、微服务消费者降级FallbackHelloService类内容

package com.yyh.service;

import com.yyh.entity.Customer;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

@Component
public class FallbackHelloService implements HelloServiceConsumer{
    @Override
    public String say() {
        return "service not----- available";
    }

    @Override
    public Customer s(@PathVariable("id") String id) {
        Customer customer = new Customer();
        customer.setFirstName("222");
        customer.setId(987);
        return customer;
    }


}

13、微服务消费者Controller

package com.yyh.controller;

import com.yyh.entity.Customer;
import com.yyh.service.HelloServiceConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/12/21 0021.
 */
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    HelloServiceConsumer helloServiceConsumer;
    @RequestMapping("/say/{id}")
    public Customer get(@PathVariable("id") String id){
        return  helloServiceConsumer.s(id);
    }
}

14、微服务消费者启动类

package com.yyh;

import com.yyh.entity.Customer;
import com.yyh.service.SayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//必不可少
@RestController
@EnableCircuitBreaker
@EnableHystrixDashboard
public class ConsulClient4App {

    @Autowired
    private SayService sayService;

    @RequestMapping("say")
    public String say() throws InterruptedException {
        return sayService.say();
    }

    @RequestMapping("s/{id}")
    public Customer s(@PathVariable("id")String id) throws InterruptedException {
        return sayService.s(id);
    }

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

15、启动服务提供者和消费者,访问

http://127.0.0.1:8014/hello/say/8564

结果如下

代码地址

https://github.com/TalkIsCheapGiveMeMoney/spring_cloud_learn/tree/master/discover_server_with_consul/springcloudlearn

猜你喜欢

转载自blog.csdn.net/zsj777/article/details/85211830