文章目錄
一.前言
在Spring Cloud 之Eureka Server中,我們介紹了Eureka Server的搭建,今天我們來搭建Eureka Client。然後完成下面的案例
二.服務生產者
2.1 創建項目
經過上面的一頓操作,我們已經成功創建項目,下面我們來看下項目結構
2.2 pom.xml文件
<?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>
<parent>
<groupId>com.milo</groupId>
<artifactId>milgenius-springcloud</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.milo</groupId>
<artifactId>springcloud-service-provider</artifactId>
<version>1.0.0</version>
<name>springcloud-service-provider</name>
<description>服務提供者</description>
<properties>
<java.version>1.8</java.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-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3 添加註解@EnableDiscoveryClient
package com.milo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
2.4 配置文件
#服務命名
spring:
application:
name: service-provider
#服務註冊中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
2.5 模擬一個服務接口
/**
* @author: Milogenius
* @create: 2019-06-28 12:14
* @description:
**/
@RestController
@Slf4j
public class ProviderController {
@Autowired
private DiscoveryClient client;
/**
* 模擬一個服務接口
* @return
*/
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(){
List<ServiceInstance> instances = client.getInstances("service-provider");
ServiceInstance instance = instances.get(0);
log.info("hello, host:" + instance.getHost()+",service_id:"+instance.getServiceId());
return "hello world";
}
}
三.服務消費者
3.1 創建項目
經過上面的一頓操作,我們已經成功創建項目,下面我們來看下項目結構
3.2 pom.xml文件
<?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>
<parent>
<groupId>com.milo</groupId>
<artifactId>milgenius-springcloud</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.milo</groupId>
<artifactId>springcloud-service-consumer</artifactId>
<version>1.0.0</version>
<name>springcloud-service-consumer</name>
<description>服務消費者</description>
<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>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.3 添加註解@EnableDiscoveryClient
package com.milo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
3.4 配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-consumer
3.5 模擬一個業務
/**
* @author: Milogenius
* @create: 2019-06-28 13:45
* @description:
**/
@RestController
public class ConsumerController {
@Autowired
private IConsumerService consumerService;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(@RequestParam String name){
return consumerService.hello(name);
}
}
/**
* @author: Milogenius
* @create: 2019-06-28 13:48
* @description:
**/
public interface IConsumerService {
String hello(String name);
}
/**
* @author: Milogenius
* @create: 2019-06-28 13:49
* @description:
**/
@Service
public class ConsumerServiceImpl implements IConsumerService {
@Autowired
RestTemplate restTemplate;
@Override
public String hello(String name) {
return restTemplate.getForObject("http://SERVICE-PROVIDER:8762/hello?name="+name,String.class);
}
}
四.服務間調用
首次我們啓動springcloud-eureka-server
Eureka Server
瀏覽器訪問http://localhost:8761/
接着我們啓動springcloud-service-provider
服務提供者Eureka Client
瀏覽器訪問http://localhost:8761/
最後,我們啓動springcloud-service-consumer
服務消費者Eureka Client
瀏覽器訪問http://localhost:8761/
至此,我們完成了項目的搭建和服務的註冊,接下來我們測試服務間的調用;
在我們大家服務消費者項目時候,我們模擬了一個業務,在ConsumerServiceImpl
類的hello
方法中我們模擬調用了另外一個服務;
瀏覽器訪問http://localhost:8764/hello?name=milogenius
我們發現,頁面成功響應helloworld;現在我們去服務生產者看一下後臺日誌;
五.結論
經過上面的學習,我們完成了Spring Cloud之Eureka的服務的註冊和發現,並完成服務之間的調用;