创建消费端/服务端
微服务的特点即一个服务即是服务端也是消费端,我们在springCloud踩坑系列-Eureka-单节点服务端一文中创建了父工程spring-cloud,现在我们在父工程下创建一个子项目:eureka-client1.其pom文件如下:
<?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.vj.study</groupId>
<artifactId>eureka-client1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-client1</name>
<description>eurekaclient project for Spring Boot</description>
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.vj.study</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>
,新建java class: eureka-client1.src.main.java.com.vj.study.eurekaclient1.EurekaClientApplication.java
代码为:
package com.vj.study.eurekaclient1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@FeignClient(name = "client2")
interface FeignClientSecond {
@RequestMapping("/ClientSecond/greet")
String greet();
}
@RestController
@RequestMapping("/ClientFirst")
class ClientFirstController{
@Autowired
private FeignClientSecond feignClientSecond;
@RequestMapping("/greet")
public String greet(){
return "hello,I`m ClientFirst, from ClientFirst";
}
@RequestMapping("/clientSecondGreet")
public String clientSecondGreet(){
return feignClientSecond.greet();
}
}
}
复制eureka-client1为eureka-client2,eureka-client1-2,我们设置client1和client1-2提供相同的服务clientFirst,client2为单独的服务clientSecond.模拟服务调用时的负载均衡.在clientSecond里面调用clientFirst的服务,看结果是否来自client-1和client1-2,在client-1和client1-2中调用clientSecond,模拟当高可用的某个服务挂掉后是否其它节点能继续提供服务.
- eureka-client1-2模块中的pom文件:
<?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.vj.study</groupId>
<artifactId>eureka-client1-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-client1-2</name>
<description>eurekaclient project for Spring Boot</description>
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.vj.study</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>
- eureka-client1-2模块中的yml配置文件:
server:
port: 6033
spring:
application:
name: client1
eureka:
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://server1:6021/eureka/,http://server2:6022/eureka/,http://server3:6033/eureka/ # 指定注册中心的地址
instance:
prefer-ip-address: false
ribbon:
eureka:
enabled: true
- eureka-client1-2模块中的EurekaClientApplication.java:
package com.vj.study.eurekaclient3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@FeignClient(name = "client2")
interface FeignClientSecond {
@RequestMapping("/ClientSecond/greet")
String greet();
}
@RestController
@RequestMapping("/ClientFirst")
class ClientFirstController{
@Autowired
private FeignClientSecond feignClientSecond;
@RequestMapping("/greet")
public String greet(){
return "hello,I`m ClientFirst, from ClientThird";
}
@RequestMapping("/clientSecondGreet")
public String clientSecondGreet(){
return feignClientSecond.greet();
}
}
}
可以看到,eureka-client1-2模块中除了端口和模块名和eureka-client1不一样之外其它都是一样的,因为这里我们将它和eureka-client1作为多节点部署.
- eureka-client2模块中的pom文件:
<?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.vj.study</groupId>
<artifactId>eureka-client2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-client2</name>
<description>eurekaclient project for Spring Boot</description>
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.vj.study</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>
- eureka-client2模块中的yml配置文件:
server:
port: 6032
spring:
application:
name: client2
eureka:
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://server1:6021/eureka/,http://server2:6022/eureka/,http://server3:6033/eureka/ # 指定注册中心的地址
instance:
prefer-ip-address: false
ribbon:
eureka:
enabled: true
- eureka-client2模块中的EurekaClientApplication.java:
package com.vj.study.eurekaclient2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@FeignClient(name = "client1")
interface FeignClientFirst {
@RequestMapping("/ClientFirst/greet")
String greet();
}
@RestController
@RequestMapping("/ClientSecond")
class ClientSecondController{
@Autowired
private FeignClientFirst feignClient;
@RequestMapping("/greet")
public String greet(){
return "hello,I`m ClientSecond, from ClientSecond";
}
@RequestMapping("/clientFirstGreet")
public String clientFirstGreet(){
return feignClient.greet();
}
}
}
运行与测试服务
访问服务注册中心,查看服务注册情况
分别运行三个模块中的EurekaClientApplication.java.我们先去eureka服务注册中心看一下现在服务的注册情况.
这里服务中心我采用了改hosts的方式部署,详情见springCloud踩坑系列-Eureka-构建高可用服务注册中心.
访问http://server1:6021/, 界面如下:
可以看到,我们服务注册中心三个节点正常运行,eureka-client1和eureka-client1-2组成了client1服务,eureka-client2注册成了client2服务,所有服务正常运行,现在我们继续进行服务调用测试.
访问服务
- 访问http://server1:6031/ClientFirst/greet:
- 访问http://server1:6031/ClientFirst/clientSecondGreet:
- 访问eureka-client1-2下的http://server1:6033/ClientFirst/greet:
- 访问eureka-client1-2下的http://server1:6033/ClientFirst/clientSecondGreet:
- 访问eureka-client2下的http://server3:6032/ClientSecond/greet:
测试负载均衡
- 访问eureka-client2下的http://server3:6032/ClientSecond/clientFirstGreet:
刷新:
说明负载均衡生效.
模拟服务掉线,测试高可用
-
访问eureka-client1-2下的http://server1:6033/ClientFirst/greet:
-
访问eureka-client2下的http://server3:6032/ClientSecond/clientFirstGreet:
刷新:
ok,高可用测试完成.
结束语
这里只是做了关于eureka服务调用功能的测试,即侧重于调用成功与否,关于feign传参等复杂调用,我们下篇见?