本文基于spring cloud Finchley.SR1

consul如何搭建可以看文章consul docker方式搭建

本文章源码位置:https://github.com/wanghongqi/springcloudconsul_test/

目录

服务端

客户端

测试


服务端

pom.xml

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

application.yml

server:
  port: 9201

spring:
  application:
    name: springtest-service
  cloud:
    consul:
      host: 192.168.140.128
      port: 8500
      discovery:
        register: true
        hostname: 10.1.69.72
        serviceName: ${spring.application.name}
        port: ${server.port}
        healthCheckPath: /home
        healthCheckInterval: 15s
        tags: urlprefix-/${spring.application.name}
        instanceId: ${spring.application.name}

 application.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class SpringtestServerApplication {

    @Autowired
    private DiscoveryClient discoveryClient;

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

    /**
     * 获取所有服务
     */
    @RequestMapping("/services")
    public Object services() {
        return discoveryClient.getServices();
    }

    @RequestMapping("/home")
    public String home() {
        return "Hello World";
    }

    @RequestMapping("/test")
    public String test(String id) {
        return "test"+id;
    }
}

客户端

pom.xml

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

application.yml

server:
  port: 9202

spring:
  application:
    name: springtest-client
  cloud:
    consul:
      host: 192.168.140.128
      port: 8500
      discovery:
        register: false

application.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class SpringtestClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringtestClientApplication.class, args);
    }
    @Autowired
    private LoadBalancerClient loadBalancer;

    @Autowired
    private DiscoveryClient discoveryClient;

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

    /**
     * 获取所有服务
     */
    @RequestMapping("/services")
    public Object services() {
        return discoveryClient.getInstances("springtest-service");
    }
}

测试

访问服务端的/services和/home

访问客户端的/services和/discover

Feign调用实现

pom.xml

    <dependencies>
        <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.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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.yml


server:
  port: 9203
feign:
  hystrix:
    enabled: true
ribbon:
  ReadTimeout:  30000
  ConnectTimeout:  15000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000

bootstrap.yml


spring:
  application:
    name: springtest-feign
  cloud:
    consul:
      host: 192.168.140.128
      port: 8500
      discovery:
        instance-id: ${spring.application.name}:${server.port}
        service-name: ${spring.application.name}
    config:
      discovery:
        enabled: true
        service-id: springcloud-config-server
      fail-fast: true

Application.java

@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class SpringtestFeignApplication {

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

MyFeignClient

@FeignClient(name = "springtest-service",fallback = HystrixFeignFallback.class)
public interface MyFeignClient {
    //这里是使用feign请求的地址
    @RequestMapping("/home")
    String home();
    @RequestMapping("/test")
    String test(@RequestParam(value = "id")String id);
}

HystrixFeignFallback  断路器

@Component
public class HystrixFeignFallback implements MyFeignClient {
    @Override
    public String home(){
        return "Hystrix home";
    }
    @Override
    public String test(String id) {
        return "Hystrix test"+id;
    }
}

TestController.java

@RestController
public class TestController {
    @Autowired
    MyFeignClient myFeignClient;
    @RequestMapping("/home")
    public String home(){
        return myFeignClient.home();
    }
    @RequestMapping("/test")
    public String test(String id){
        return myFeignClient.test(id);
    }
}

测试

/home

/test?id=123

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐