本文包含4个springboot服务,A.服务注册中心,B.被发现和被调用服务,C.接口服务 ,D. client服务;
C服务调用B 服务,若是C调用B服务失败,有断路功能,并且C 服务对外提供接口供D 服务使用
注意:示例只提供基本的逻辑,不做业务处理。
各个服务结构图如下:
pom文件:
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
application yml文件:
server:
port: 20001
eureka:
instance:
hostname: 127.0.0.1
appname: eureka-server #服务注册中心名称
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port} #服务注册中心IP
registerWithEureka: false #不能注册自身
fetchRegistry: false
启动类 java 文件:
@SpringBootApplication
@EnableEurekaServer//服务注册server注解
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
pom 文件:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-config
2.1.2.RELEASE
org.springframework.cloud
spring-cloud-starter-eureka
1.4.6.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-config-client
2.1.2.RELEASE
org.springframework.boot
spring-boot-starter-test
test
application yml文件:
eureka:
instance:
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://127.0.0.1:20001/eureka/ #注册中心地址
spring:
application:
name: organizationservice
profiles:
active:
default
cloud:
config:
enabled: true
server:
port: 8087
服务启动Java文件:
@SpringBootApplication
@EnableEurekaClient//服务发现注解
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
控制层 Java文件:
@RestController
public class Hello {
@GetMapping("/hello")
public User hello() {
User user = new User();
user.setAge("66");
user.setName("abcd");
return user;
}
}
pom 文件:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-starter-openfeign
2.0.2.RELEASE
com.netflix.hystrix
hystrix-javanica
1.5.12
application yml文件:
eureka:
instance:
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:20001/eureka/ #注册中心地址
spring:
application:
name: licensingservice
profiles:
active:
default
cloud:
config:
enabled: true
server:
port: 8088
#打开断路器
feign:
hystrix:
enabled: true
启动类 java文件:
/**
* 接口应用
* 发现 server 服务,调用 server服务应用的api
* 不处理逻辑业务
* 对外提供接口
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class LicenseApplication {
public static void main(String[] args) {
SpringApplication.run(LicenseApplication.class, args);
}
}
接口类:
//TestCallBack--- 若是调用organizationservice服务接口失败,调用TestCallBack类中相同的接口方法
@FeignClient(name = "organizationservice", fallback = TestCallBack.class)
public interface TestApi {
@GetMapping("/hello")
User hello();
}
断路器类:
@Component
public class TestCallBack implements TestApi {
@Override
public User hello() {
return null;
}
}
控制类:
@RestController
public class TestController {
@Autowired
TestService testService;
@GetMapping("/hello")
public User hello () {
return testService.hello();
}
}
业务类:
@Service
public class TestService {
@Autowired
TestApi testApi;
public User hello() {
return testApi.hello();
}
}
pom 文件:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-thymeleaf
com.alibaba
fastjson
1.2.28
application 文件:
server.port=8889
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
启动类: 不需要修改,默认
控制类:
@RestController
public class TestController {
@Autowired
private TestService service;
@GetMapping("/hello")
public User getHello () {
return service.getHello();
}
}
业务类:
@Service
public class TestService {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate template;
public User getHello () {
String url = "http://localhost:8088/hello";
ResponseEntity resultResponseEntity = this.template.exchange(
String.format(url),
HttpMethod.GET, null, User.class);
if (resultResponseEntity.getStatusCode() == HttpStatus.OK) {
return resultResponseEntity.getBody();
}
return null;
}
}
进入服务注册中心,出现如下标注处的服务,说明服务已经注册到了注册中心:
进入D 服务,能够返回数据,说明请求成功:
进入C服务调用接口:
关闭服务B,再次调用服务C接口:
返回内容为空,说明断路器起了作用。