spring cloud分為註冊端、客戶端以及消費端 初體驗的理解就是: 註冊端就是將之前所有的應用在這邊進行註冊,然後給每個應用都生成自己的標識,這些應用就是來自於客戶端,消費端則通過調用註冊端(有點數據中心的概念)然後去調用客戶端各個的應用. 1、註冊端代碼: 直接創建maven項目: pom. ...
spring cloud分為註冊端、客戶端以及消費端
初體驗的理解就是:
註冊端就是將之前所有的應用在這邊進行註冊,然後給每個應用都生成自己的標識,這些應用就是來自於客戶端,消費端則通過調用註冊端(有點數據中心的概念)然後去調用客戶端各個的應用.
1、註冊端代碼:
直接創建maven項目:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tp.soft</groupId> <artifactId>cloud-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-service</name> <description>Forward project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</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>Finchley.SR1</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>
application.yml
#本項目埠
server:
port: 8761
#服務註冊中心實例主機名
eureka:
instance:
hostname: host
client:
#是否向註冊中心註冊自己
register-with-eureka: false
#是否獲取註冊表
fetch-registry: false
#服務地址,向哪裡註冊的地址,如果沒有下麵的註冊地址但是開啟了上面的
#想註冊中心註冊自己就會報錯,因為沒有說明註冊中心地址,不知道註冊到哪裡
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
#通過引用上面的定義來聲明註冊地址,有一定局限性主要在hostname這塊,上面寫死的方式比較靈活,如果一定要用下麵這種方法,必要時hostname的映射一定要匹配地址,需要在host文件中做映射
# defaultZone: http://${eureka.instance.hostname}:{server.port}/eureka/
CloudServerApplication.java
package com.tp.soft.cloudserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class CloudServerApplication { public static void main(String[] args) { SpringApplication.run(CloudServerApplication.class, args); } }
啟動springboot
至此註冊中心搭建完成,目前看到的是0個應用
2、客戶端代碼:
同樣創建一個maven項目
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tp.soft</groupId> <artifactId>cloud-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</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>Finchley.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>
application.yml
spring:
application:
name: client
server:
port: 8762
eureka:
client:
eureka-server-port: 8762
register-with-eureka: true
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
CloudClientApplication.java
package com.tp.soft.cloudclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class CloudClientApplication { public static void main(String[] args) { SpringApplication.run(CloudClientApplication.class, args); } }
創建應用介面 也就是後期所有業務介面源頭 註冊到註冊中心供其他業務互相調用
HelloController.java
package com.tp.soft.cloudclient.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("hello") public String hello(){ return "hello"; } }
啟動之後
可以看到client應用已經註冊成功,接下來就可以在消費端去直接訪問這個應用 不用再重覆相同的業務介面了
3、消費端
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tp.soft</groupId> <artifactId>cloud-consume</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-consume</name> <description>Forward project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</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>Finchley.SR1</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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.2.RELEASE</version> </dependency> <!--斷路器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.4.RELEASE</version> </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>
application.yml
#本項目埠
server:
port: 8763
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
spring:
application:
name: service-ribbon
CloudConsumeApplication.java
package com.tp.soft.cloudconsume; 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 CloudConsumeApplication { public static void main(String[] args) { SpringApplication.run(CloudConsumeApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } }
HelloController.java
package com.tp.soft.cloudconsume.controller; 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 HelloController { @Autowired RestTemplate restTemplate; @GetMapping("hi") public String hi(){ return restTemplate.getForObject("http://CLIENT/hello", String.class); } }
http://對應的應用名/介面名
項目鏈接
https://github.com/tplovejava/springcloud.git
至此完成配置,有什麼不足的,請大牛們不令賜教