Spring Cloud - 服務(wù)發(fā)現(xiàn)仗扬、注冊(cè)、調(diào)用
- Spring Cloud - 服務(wù)發(fā)現(xiàn)蕾额、注冊(cè)厉颤、調(diào)用(一)(eureka server)
- Spring Cloud - 服務(wù)發(fā)現(xiàn)、注冊(cè)凡简、調(diào)用(二)(eureka client(provider))
-
Spring Cloud - 服務(wù)發(fā)現(xiàn)逼友、注冊(cè)、調(diào)用(三)(eureka client(consumer))
代碼參見(jiàn)https://github.com/happut/springbootdemo
eureka client (consumer)
下面我們編寫(xiě)下服務(wù)調(diào)用例子:
建立springboot項(xiàng)目秤涩,增加spring-cloud-starter-eureka-server
起步依賴(lài):
<?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.github.happut</groupId>
<artifactId>springboot-demo-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-demo-eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啟動(dòng)代碼中增加@EnableEurekaClient
以及RestTemplate
@SpringBootApplication
@EnableEurekaClient
public class SpringbootDemoEurekaClientApplication {
@LoadBalanced
@Bean
public RestTemplate createRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoEurekaClientApplication.class, args);
}
}
application.properties
(yml請(qǐng)注意縮進(jìn))
server.port=1113
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
這里需要注意下端口不要和主服務(wù)器沖突帜乞,否則無(wú)法啟動(dòng)。
spring.application.name
代表服務(wù)名稱(chēng)筐眷,注意不要和服務(wù)提供者一致黎烈,否則接口調(diào)用的時(shí)候就懵逼了。
eureka.client.serviceUrl.defaultZone
務(wù)必要和主服務(wù)器一致匀谣,否則不知道去哪里拉取服務(wù)照棋。
在這里為了演示,我們嘗試調(diào)用服務(wù)提供者的接口:
首先武翎,如果不用spring cloud eureka烈炭,咱們一般這么寫(xiě):
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/hello")
public String index() {
return restTemplate.getForObject("http://localhost:1112/hello", String.class);
}
}
可以看到,我們的服務(wù)器提供者的地址被硬編碼到代碼中宝恶,這可不好符隙,萬(wàn)一1112服務(wù)器down了,或者服務(wù)器遷移還得改代碼垫毙。
當(dāng)我們用了spring cloud框架后會(huì)怎樣呢霹疫,代碼這么寫(xiě):
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/hello")
public String index() {
return restTemplate.getForObject("http://hello-service-provider/hello", String.class);
}
}
可以看到一些變化,地址不是ip端口综芥,而是改為http://服務(wù)名稱(chēng)/接口路由丽蝎。
服務(wù)名稱(chēng)!0蛎辍屠阻!
服務(wù)名稱(chēng):焓 !栏笆!
服務(wù)名稱(chēng)@嗳k蛉加!
(不會(huì)告訴你我被這個(gè)坑了好幾天)
然后樂(lè)呵啟動(dòng),看下主服務(wù)器中的變化:
我們可以簡(jiǎn)單測(cè)試下接口:
接口也調(diào)用成功了喲缸逃,是1113服務(wù)通過(guò)主服務(wù)器調(diào)用的1112的接口针饥,神奇吧。