此次操作,共计4个项目。 两个注册中心构成集群:server server02 一个服务提供者:server-provider 一个服务消费者 :server-consumer
首先搭建server 项目。
pom.xml如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.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>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<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.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>
application.yml如下:
server:
port: 8761
eureka:
instance:
#使用IP注册
prefer-ip-address: true
#ip-address: 192.168.1.1 #强制指定IP地址,默认会获取本机的IP地址
instance-id: ${spring.cloud.client.ip-address}:${server.port}
hostname: ${spring.cloud.client.ip-address}
client:
serviceUrl:
defaultZone: http://${spring.cloud.client.ip-address}:8762/eureka/
spring:
application:
name: server01
启动类如下:
package com.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurakeServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurakeServerApplication.class , args);
}
}
第二个项目:server02
pom.xml同项目一 server
application.yml如下:
server:
port: 8762
eureka:
instance:
#使用IP注册
prefer-ip-address: true
#ip-address: 192.168.1.1 #强制指定IP地址,默认会获取本机的IP地址
instance-id: ${spring.cloud.client.ip-address}:${server.port}
hostname: ${spring.cloud.client.ip-address}
client:
serviceUrl:
defaultZone: http://${spring.cloud.client.ip-address}:8761/eureka/
spring:
application:
name: server02
启动类如下:
package com.server02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurakeServerApplication02 {
public static void main(String[] args) {
SpringApplication.run(EurakeServerApplication02.class , args);
}
}
然后创建服务提供者, 具体如下:
pom.xml如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.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>Hoxton.SR8</spring-cloud.version>
</properties>
<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>
</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>
application.yml如下:
eureka:
instance:
#使用IP注册
prefer-ip-address: true
#ip-address: 192.168.1.1 #强制指定IP地址,默认会获取本机的IP地址
instance-id: ${spring.cloud.client.ip-address}:${server.port}
hostname: ${spring.cloud.client.ip-address}
client:
serviceUrl:
defaultZone: http://${spring.cloud.client.ip-address}:8761/eureka/,http://${spring.cloud.client.ip-address}:8762/eureka/
server:
port: 7070
spring:
application:
name: provider-server
启动类如下:
package com.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class providerApplication {
public static void main(String[] args) {
SpringApplication.run(providerApplication.class , args);
}
}
Controller层如下:
package com.provider.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("demo")
public class DemoController {
@GetMapping("/privider")
public String getInfo()
{
return "666";
}
}
最后, 创建消费者 ,具体如下:
pom.xml如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.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>Hoxton.SR8</spring-cloud.version>
</properties>
<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>
</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>
application.yml如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ #注册中心地址
register-with-eureka: false #是否将自己注册到注册中心, 默认是true
registry-fetch-interval-seconds: 10 #表示Erueka Client 间隔多久去服务器拉取注册信息 , 默认30秒
server:
port: 9090
spring:
application:
name: consumer-server
RestTemplateConfig配置类如下:
package com.consumer.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Controller层如下:
package com.consumer.controller;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("consumerDemo")
public class ConsumerDemo {
@Autowired
protected RestTemplate restTemplate;
//元数据对象的调用方式
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/consumer")
public String getConsumer()
{
List<ServiceInstance> serverList = discoveryClient.getInstances("PROVIDER-SERVER");
ServiceInstance server = serverList.get(0);
String url = "http://" + server.getHost() + ":" + server.getPort() + "/demo/privider";
System.out.println(url);
String resultStr = restTemplate.getForObject(url , String.class);
return resultStr;
}
}
最后将项目都启动, 然后直接访问消费者项目, 就能获取服务提供者提供的服务。 整个过程消费者和服务者通过注册中心联系。
这些代码留着给自己做备份。