Spring cloudはconsul、feign、hystrixを統合します.
サービス:
pom.xmlファイル:
pomファイル:
1、先は【https://www.consul.io/downloads.html)consulサービスをダウンロードし、解凍して起動します.(window起動:cdからconsulまでの解凍のカタログで実行:consul agent-dev)
2、起動後にホームページを開く【http://localhost:8500/ui/dc1/servicesを選択します
3、サービスポートを変更して、二つのサービス端末を起動する(負荷の均衡を実証するために【controllerのいくつかの戻り値と印刷値を修正して、どのサービス端末を呼び出したのか分かりやすいようにする】
4、クライアントサービスの実行を開始する:http://localhost:8503/servicesを選択しますhttp://localhost:8503/callを選択しますhttp://localhost:8503/feign/testを選択しますhttp://localhost:8503/hystrix/test
5、二つのサービスを切り離して実行する:http://localhost:8503/feign/testを選択しますhttp://localhost:8503/hystrix/test溶断機構が発効していることが分かった.
pom.xmlファイル:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.snd
consul
0.0.1-SNAPSHOT
consul
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
propertiesファイル:#
spring.application.name=spring-cloud-consul-producer-one
#
server.port=8501
#consul~IP
spring.cloud.consul.host=localhost
#consul~
spring.cloud.consul.port=8500
# consul
spring.cloud.consul.discovery.serviceName=consul-service-producer
controlerテスト:package com.snd.consul.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/hello")
public String test() {
System.out.println("TestController==>hello consul one");
return "hello consul one";
}
}
ブートクラス:package com.snd.consul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* consul
* @author Jobs
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class, args);
}
}
クライアント:pomファイル:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.snd
consul-consumer
0.0.1-SNAPSHOT
consul-consumer
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-hystrix
1.4.0.RELEASE
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
1.4.0.RELEASE
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
propertiesファイル:spring.application.name=spring-cloud-consul-consumer
server.port=8503
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
# consul
spring.cloud.consul.discovery.register=false
# consul
#spring.cloud.consul.discovery.serviceName=consul-service-producer
#
feign.hystrix.enabled=true
ブートクラス:package com.snd.consul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class ConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class, args);
}
}
controler層:package com.snd.consul.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServiceController {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private DiscoveryClient discoveryClient;
/**
*
*/
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances("consul-service-producer");
}
/**
* ( )
*/
@RequestMapping("/discover")
public Object discover() {
return loadBalancer.choose("consul-service-producer").getUri().toString();
}
}
package com.snd.consul.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.snd.consul.service.FeignClientService;
@RestController
public class TestController {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private FeignClientService feignClientService;
@RequestMapping("/call")
public String call() {
ServiceInstance serviceInstance = loadBalancer.choose("consul-service-producer");
System.out.println(" :" + serviceInstance.getUri());
System.out.println(" :" + serviceInstance.getServiceId());
String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
System.out.println(callServiceResult);
return callServiceResult;
}
@GetMapping("/feign/test")
public String feign() {
return feignClientService.testHello();
}
@HystrixCommand(fallbackMethod="hystrixTest")
@GetMapping("/hystrix/test")
public String hystrix() {
System.out.println("/hystrix/test");
ServiceInstance serviceInstance = loadBalancer.choose("consul-service-producer");
String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
return callServiceResult;
}
public String hystrixTest() {
return " , hystrix , !";
}
}
サービス層:package com.snd.consul.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import com.snd.consul.service.impl.FallbackServiceImpl;
//@FeignClient("spring-cloud-consul-producer-two")
@FeignClient(value="consul-service-producer", fallback=FallbackServiceImpl.class)
public interface FeignClientService {
@GetMapping("/hello")
String testHello();
}
package com.snd.consul.service.impl;
import org.springframework.stereotype.Component;
import com.snd.consul.service.FeignClientService;
/**
*
* @author Jobs
*
*/
@Component
public class FallbackServiceImpl implements FeignClientService {
@Override
public String testHello() {
return " - -- ====1111!";
}
}
ステップ:1、先は【https://www.consul.io/downloads.html)consulサービスをダウンロードし、解凍して起動します.(window起動:cdからconsulまでの解凍のカタログで実行:consul agent-dev)
2、起動後にホームページを開く【http://localhost:8500/ui/dc1/servicesを選択します
3、サービスポートを変更して、二つのサービス端末を起動する(負荷の均衡を実証するために【controllerのいくつかの戻り値と印刷値を修正して、どのサービス端末を呼び出したのか分かりやすいようにする】
4、クライアントサービスの実行を開始する:http://localhost:8503/servicesを選択しますhttp://localhost:8503/callを選択しますhttp://localhost:8503/feign/testを選択しますhttp://localhost:8503/hystrix/test
5、二つのサービスを切り離して実行する:http://localhost:8503/feign/testを選択しますhttp://localhost:8503/hystrix/test溶断機構が発効していることが分かった.