簡介:目前2.x版本的springcloud和以前1.x版本在依賴、註解有一些區別,以下是用2.1.7版本的springboot,2.0.0的springcloud,這裏簡單介紹搭建springcloud入門。
1.建立註冊中心項目(eureka-server)
idea建立步驟:File ->New -> project -> Spring initializr ->點擊next->填寫artifactId> (原作者爲2.0.3版本,最後有原文鏈接)
點擊next,選擇上圖中選項,finish。
1)引入依賴包:
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ali77</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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)啓動類加上@EnableEurekaServer表示這是一個註冊中心項目:
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
3)application.yml配置
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
配置端口爲8761,register-with-eureka:false表示本身不註冊,fetch-registry:fasle表示不從Eureka server獲取註冊信息,
配置的defaultZone爲註冊地址和交互地址(若部署多臺註冊中心,配置用“,”分割)。
4)啓動項目,訪問http://localhost:8761,可以看到這樣的界面,
表示成功。
2.建立eureka-client接口服務提供項目(生產者)
1)項目建立步驟和第一步相似,pom依賴爲:
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ali77</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ali77</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2)啓動類代碼
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
加入EnableDiscoveryClient註解表示向註冊中心註冊。
3)建立一個TestController,代碼爲:
@RestController
public class TestController {
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}
提供一個接口用來測試。
4)application.yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: eureka-client
配置註冊中心地址和自己的項目名稱和端口。
5)啓動項目,然後刷新第一步註冊中心地址:http://localhost:8761,可以看到:
說明已經註冊成功了。
3.建立第二個client項目
爲了方便後面測試負載均衡,拷貝第二步生成的項目,或者重複建立上一個項目,只改項目名和端口,將application.yml配置的端口改爲8763,其餘不變,然後啓動。
可以看到:
說明已經有兩臺EUREKA-CLIENT項目註冊到註冊中心。
4.使用Feign做負載均衡
1)建立步驟相似,項目名稱爲:eureka-feign
2)pom依賴
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
3)啓動類
@EnableDiscoveryClient//向服務中心註冊
@SpringBootApplication
@EnableFeignClients
public class EurekaRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
}
EnableFeignClients表示開啓FeignClients,後面會在接口上用到@FeignClient
4)application.yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: eureka-feign
同樣註冊到註冊中心,端口爲8764
5)準備調用client中的接口
建立一個接口HelloRemote,用來調用eureka-client裏的接口
@FeignClient(name= "eureka-client")
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
一個控制層類HelloController,暴露接口:(如果Autowired報錯helloRemote,是idea的檢查錯誤,百度方法去掉就好,不影響)
@RestController
public class HelloController {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name);
}
}
啓動項目,可以看到註冊中心中多了一個eureka-feign項目
6)調用接口
訪問 http://localhost:8764/hello/Capricornce,可以看到:
多次刷新,還能看到:
這樣就使用feign達到了負載均衡的效果。
後續的熔斷監控、springboot admin 這邊先不加了。
本文爲轉載,參照操作,基本很順利,特意轉載,記錄一下,本人蔘照實現版本爲SpringBoot2.1.7,原文爲2.0.3,沒什麼影響,感謝原創。
轉載請標註原文地址:https://blog.csdn.net/qq_36758630/article/details/80986902
版權聲明:本文爲CSDN博主「汪牽牽」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_36758630/article/details/80986902