参考URL:<https://zhuanlan.zhihu.com/p/272663162?utm_id=0>
■紹介
SpringCloudの初心者に向け、簡単な手順を作成する。Eurekaサーバを利用して、「server」を立って。それにして、
提供者(provider)と消費者(consumer)を全て「server」に導入して、管理する。消費者(consumer)の内部に、ribbonの技術を利用して、
提供者(provider)を呼びます。三つのプロジェクトを別々に構築できるし、「server」で管理できる。
■構造
■説明
・spring-cloud-eureka-server:子項目(Module)、Eurekaのサービスを提供する
・spring-cloud-service-provider:子項目(Module)、Eurekaに導入し、提供者としてサービスを提供
・spring-cloud-client-ribbon:子項目(Module)、Eurekaに導入し、消費者として、内部で提供者に呼ぶ
■「POM.xml」の配置
・spring-cloud-eureka-server
<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>org.example</groupId> <artifactId>spring-cloud-eureka-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-server</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- SpringBoot1.5.x --> <spring.cloud.dependencies.version>Edgware.SR5</spring.cloud.dependencies.version> </properties> <!-- 引入SpringBoot-Parent项目 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <dependencies> <!-- Cloud Server依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>pom.xml
・spring-cloud-service-provider
<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>org.example</groupId> <artifactId>spring-cloud-service-provider</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-service</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.cloud.dependencies.version>Edgware.SR5</spring.cloud.dependencies.version> </properties> <!-- 引入SpringBoot-Parent项目 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <dependencies> <!-- Cloud Client依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>pom.xml
・spring-cloud-client-ribbon
<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>org.example</groupId> <artifactId>spring-cloud-client-consumer</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-client-ribbon</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.cloud.dependencies.version>Edgware.SR5</spring.cloud.dependencies.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <dependencies> <!-- Cloud Client依赖 --> <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-ribbon</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>pom.xml
■「application.xml」の配置
・spring-cloud-eureka-server
server: port: 8700 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #如果需要开通多个端口时,使用下面方式 # defaultZone: http://localhost:8699/eureka,http://localhost:8698/eureka spring: application: name: eureka-serverapplication.xml
・spring-cloud-service-provider
server: port: 8701 eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:8700/eureka spring: application: name: eureka-providerapplication.xml
・spring-cloud-client-ribbon
server: port: 8702 eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:8700/eureka spring: application: name: eureka-consumer eureka-provider: ribbon: ActiveConnectionsLimit: 1000application.xml
■「XXXApplication.java」の配置
・spring-cloud-eureka-server
package org.example; 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); } }CloudServerApplication.java
・spring-cloud-service-provider
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class CloudServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(CloudServiceProviderApplication.class,args); } }CloudServiceProviderApplication.java
・spring-cloud-client-ribbon
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class CloudClientRibbonApplication { public static void main(String[] args) { SpringApplication.run(CloudClientRibbonApplication.class,args); } }CloudClientRibbonApplication.java
■「controller」の作成
・spring-cloud-service-provider(提供者側)
package org.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/provider") public class ProviderController { @RequestMapping("/hello") public String helloWorld(String str){ System.out.println("str:"+str); return "parameter:"+str; } }ProviderController.java
・spring-cloud-client-ribbon(消費者側)
package org.example.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/consumer") public class ConsumerController { @Autowired private LoadBalancerClient loadBalancerClient; @Autowired private RestTemplate restTemplate; @RequestMapping("/hello") public String helloWorld(@RequestParam String str){ System.out.println("str:"+str); String forObject = ""; try{ //第一种(需要bean注入,通过用Service名调用) forObject = restTemplate.getForObject("http://eureka-provider/provider/hello?str="+str,String.class); //第二种(只能通过IP+端口调用) //forObject = new RestTemplate().getForObject("http://localhost:8701/provider/hello?str="+str,String.class); //第三种(通过解析Service名转换IP+端口调用) //ServiceInstance serviceInstance =loadBalancerClient.choose("eureka-provider"); //forObject = new RestTemplate().getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/provider/hello?str="+str, String.class); }catch (Exception ex){ throw ex; } return forObject; } }ConsumerController.java
package org.example.beans; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class Beans { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }Beans.java
■入口
S1:Eurekaのサーバを確認する(http://localhost:8700)
S2:提供者(provider)のサーバを確認する(http://localhost:8701/provider/hello?str=こんにちは)
S3:消費者(consumer)を確認する(http://localhost:8702/consumer/hello?str=天気予報)
ここまで終わり
标签:spring,構築,restTemplate,springframework,eureka,Spring,org,import,cloud From: https://www.cnblogs.com/lnsylt/p/17849675.html