SpringCloud Eureak整合Ribbon经验总结
使用Eureka注册中心整合Ribbon
文章目录
前言
使用Eureak注册中心整合Ribbon,使用一个Eureka注册中心,2个Eureka客户端(包括一个服务提供方,一个服务消费客户端)
一、搭建服务注册中心和客户端
使用idea搭建一个多module模块,代码结构如下:
server -注册中心
provider-服务提供方
client-客户端
commom-公用类
二、代码说明
1.引入库
父类pom设置
<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.zz</groupId>
<artifactId>hystrix</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>common</module>
<module>provider</module>
<module>client</module>
<module>server</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</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>2021.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</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>
注册中心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">
<parent>
<artifactId>hystrix</artifactId>
<groupId>com.zz</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>server</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
配置注册中心application.properites
#服务名称
spring.application.name=server
server.port=8080
management.endpoints.web.exposure.include=*
#作为Eureka注册中心不注册自己
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
启动类增加注册@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
注册中心到此结束
配置服务提供方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">
<parent>
<artifactId>hystrix</artifactId>
<groupId>com.zz</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>服务提供方</description>
<artifactId>provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.zz</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
服务方启动类 增加注解@EnableDiscoveryClient代表向注册中心注册
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
服务提供方applicaiton.properties配置
#服务名称
spring.application.name=provider
server.port=8081
management.endpoints.web.exposure.include=*
#配置注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8080/eureka
编写服务方controller 此处随意编写只要是一个rest请求
@RestController
public class ProviderUserController {
@Autowired
private UserService userService;
@PostMapping("/user/save")
public String saveUser(@RequestBody User user) {
userService.addUser(user);
return user.toString();
}
}
服务方介绍完毕
客户端调用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">
<parent>
<artifactId>hystrix</artifactId>
<groupId>com.zz</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<description>客户端</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.zz</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
客户端application.properties
#应用名称
spring.application.name=client
server.port=8082
management.endpoints.web.exposure.include=*
server.provider.name=provider
eureka.client.service-url.defaultZone=http://localhost:8080/eureka
客户端启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
//此处使用LoadBalance代表使用Riboon进行客户端负载均衡
@Bean
@LoadBalanced
public RestTemplate getRest() {
return new RestTemplate();
}
}
客户端调用服务端代码实现类
@RestController
public class ClientUserController {
//服务提供方的spring.application.name
@Value("${server.provider.name}")
private String providerName;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/client/get")
public String getUser() {
User user = new User();
user.setId(1L);
user.setName("zz");
// String o = restTemplate.postForObject("http://" + "localhost:8081" + "/user/save", user, String.class);
String o = restTemplate.postForObject("http://" + providerName + "/user/save", user, String.class);
return o;
}
}
注册中心
进行测试访问
http://localhost:8082/client/get 即客户端请求地址,通过服务名称能正常调用到服务提供方 此处只是简单使用可以启动多个服务提供方进行Ribbon的负载均衡测试
问题处理
**在使用过程中如通过服务名调用时提示
No instances available for xx这种错误的时检查客户端pom文件是否引入了
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
本文中使用的eureka-client版本为3.3.1, 已经内置了ribbon.如果再次引入上方spring-cloud-starter-netflix-ribbon则会提示No instances available for xx
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
其他高级特性等待继续学习