本文中有三个角色,分别是服务的提供者,服务的消费者,注册中心Eureka
整体流程:
(1)先启动注册中心Eureka
(2)启动服务的提供者,将服务注册到注册中心Eureka上对外提供服务
(3)启动服务的消费者,在注册中心中找到服务并完成消费
1、服务提供者
(1)pom.xm
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>producer</name>
<description>Demo project for Spring Boot</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>
<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>
(2)配置文件application.yml
server:
port: 8090
spring:
application:
name: spring-cloud-producer
eureka:
server:
enable-self-preservation: false #关闭eureka的自我保护,防止已被关停的节点也错误的显示在线
client:
register-with-eureka: true #否允许客户端向Eureka 注册表获取信息,服务器为设置为false,客户端设置为true
fetch-registry: true #是否允许向Eureka Server注册信息 如果是服务器端,应该设置为false
service-url:
defaultZone: http://peer1:8761/eureka/ #此eureka server的应用注册地址
(3)启动类ProducerApplication.java
@SpringBootApplication
@EnableEurekaClient //如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
(4)Controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",producer is ready";
}
}
2、服务消费者
(1)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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>consumers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumers</name>
<description>Demo project for Spring Boot</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.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>
<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>
(2)application.yml
server:
port: 8091
spring:
application:
name: spring-cloud-consumers
eureka:
server:
enable-self-preservation: false #关闭eureka的自我保护,防止已被关停的节点也错误的显示在线
client:
register-with-eureka: true #否允许客户端向Eureka 注册表获取信息,服务器为设置为false,客户端设置为true
fetch-registry: true #是否允许向Eureka Server注册信息 如果是服务器端,应该设置为false
service-url:
defaultZone: http://peer1:8761/eureka/ #此eureka server的应用注册地址
(3)启动类ConsumersApplication.java
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //这个注解是通知SpringBoot在启动的时候,扫描被 @FeignClient 修饰的类
public class ConsumersApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumersApplication.class, args);
}
}
(4)Feign远程调用,创建一个remote接口
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}
(5)Controller
@RestController
public class HelloController {
@Autowired
private HelloRemote helloRemote;
@RequestMapping("/hello")
public String hello(@RequestParam("name") String name) {
return helloRemote.hello(name);
}
}
依次启动eureka,producer,consumer三个项目,
在浏览器输入http://localhost:8090/hello?name=springcloud,可以看到producer启动正常。
在浏览器输入http://localhost:8091/hello?name=spring,可以看到客户端成功通过feign调用了远程服务hello。
负载均衡
复制一份producer,修改名称为producer2,同时修改相应pom文件,修改application.yml中端口号为8092,修改Controller如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",producer2 is ready";
}
}
启动producer2,可以看到应用名为spring-cloud-producer的有两台机器。
我们再去访问消费端的http://localhost:8091/hello?name=spring,可以看到producer和producer2轮流打印,说明注册中心提供了服务负载均衡功能。