1、結構概述
本文包含4個springboot服務,A.服務註冊中心,B.被發現和被調用服務,C.接口服務 ,D. client服務;
C服務調用B 服務,若是C調用B服務失敗,有斷路功能,並且C 服務對外提供接口供D 服務使用
注意:示例只提供基本的邏輯,不做業務處理。
各個服務結構圖如下:
服務A:
pom文件:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!--服務註冊server,服務註冊中心 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
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);
}
}
服務B:
pom 文件:
<dependencies>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
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;
}
}
服務C:
pom 文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency><!-- 服務健康狀態監測 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!-- 支持斷路器-->
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.12</version>
</dependency>
</dependencies>
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();
}
}
服務D:
pom 文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
</dependencies>
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<User> resultResponseEntity = this.template.exchange(
String.format(url),
HttpMethod.GET, null, User.class);
if (resultResponseEntity.getStatusCode() == HttpStatus.OK) {
return resultResponseEntity.getBody();
}
return null;
}
}
服務 A、B、C、D 建立好了之後,按順序依次啓動這幾個服務。
進入服務註冊中心,出現如下標註處的服務,說明服務已經註冊到了註冊中心:
進入D 服務,能夠返回數據,說明請求成功:
進入C服務調用接口:
關閉服務B,再次調用服務C接口:
返回內容爲空,說明斷路器起了作用。