本文中有三個角色掷豺,分別是服務的提供者当船,服務的消費者默辨,注冊中心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的自我保護暮刃,防止已被關停的節(jié)點也錯誤的顯示在線
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的自我保護奕纫,防止已被關停的節(jié)點也錯誤的顯示在線
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遠程調用灵汪,創(chuàng)建一個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輪流打印塞耕,說明注冊中心提供了服務負載均衡功能扫外。