公共依賴: 1、創建eureka-server註冊中心工程 a、eureka-server工程pom依賴: b、eureka-server啟動類: c、eureka-server工程配置文件:eureka-server\src\main\resources\bootstrap.yml d、啟動註冊中 ...
公共依賴:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </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>Finchley.RELEASE</spring-cloud.version> </properties> <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>
1、創建eureka-server註冊中心工程
a、eureka-server工程pom依賴:
<!--加上上面的公共依賴-->
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
b、eureka-server啟動類:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
c、eureka-server工程配置文件:eureka-server\src\main\resources\bootstrap.yml
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
d、啟動註冊中心
mvn spring-boot:run
2、創建demo-client服務工程
a、demo-client工程pom配置:
<!--加上上面的公共依賴-->
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
b、demo-client啟動類:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }
c、demo-client工程配置文件:demo-client\src\main\resources\bootstrap.yml
server:
port: 7070
spring:
application:
name: client
eureka:
client:
serviceUrl:
defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8888}/eureka/
instance:
prefer-ip-address: true
d、編寫測試介面:
import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 測試API */ @RestController public class TestController { @GetMapping("/testApi") public String testApi(Integer a, Integer b, HttpServletRequest request) { return " From Port: " + request.getServerPort() + ", Result: " + (a + b); } }
e、啟動工程
mvn spring-boot:run
mvn spring-boot:run -Dserver.port=7171
啟動了2個工程實例,一個埠是7070,一個是7171
3、創建demo-ribbon客戶端工程
a、demo-ribbon工程pom依賴:
<!--加上上面的公共依賴-->
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
b、demo-ribbon工程啟動類:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class RibbonLoadbalancerApplication { public static void main(String[] args) { SpringApplication.run(RibbonLoadbalancerApplication.class, args); }
//聲明該RestTemplate用於負載均衡 @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
c、編寫一個測試介面:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Autowired private RestTemplate restTemplate; @GetMapping("/testApi") public String testApi(Integer a, Integer b) {
//使用RestTemplate調用demo-client工程暴露的測試API介面 String result = restTemplate.getForObject("http://CLIENT/testApi?a=" + a + "&b=" + b, String.class); System.out.println(result); return result; } }
d、demo-ribbon工程配置文件:demo-ribbon\src\main\resources\bootstrap.yml
spring:
application:
name: ribbon-loadbalancer
server:
port: 7777
eureka:
client:
serviceUrl:
defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8888}/eureka/
instance:
prefer-ip-address: true
e、啟動demo-ribbon工程:
mvn spring-boot:run
4、訪問 localhost:7777/testApi?a=2019&=2020
從結果可以看到這次訪問的是7171埠服務。
從下麵的控制台輸出可以看出,ribbon在做負載均衡的時候,預設使用的是輪詢的方式!
入門成功!!!