继续上篇的eureka注册中心集群搭建完 , 我们接下来搭建服务注册与发现机制
服务注册 , 我们可以理解为服务提供者 provider
发现机制,我们可以理解为消息消费者 consumer
继注册中心搭建好了后 , 继续在哪个maven里添加module
再创建一个Spring Boot工程 provider
勾选如下依赖
定义application.properties配置文件
#定义名字
spring.application.name=provider
#要注册到的注册中心的路径
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
#设置一下端口
server.port=8082
一样在启动类里添加一个注解 , 主要和注册中的注解不一样的 @EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
然后我们写个接口 , 等下让消费者来发现 , 也就是来调用这个接口
@RestController
public class HelloController {
//这里获取启动项目的端口号 ,后面用来测试这里的集群是否搭建成功
@Value("${server.port}")
String port;
@GetMapping("/hello")
public String hello(){
return "hello>>"+port;
}
}
然后启动项目 , 从注册中心上我们就会发现这个
这个provider的集群 和注册中心的类似 , 也是打包 ,然后进入目录去启动两个 , 只不过这里用来区分的是后面的端口不一样 , 如下
然后我们在去写个发现消息的消费者 consumer
同样在maven项目区新建一个module
也是Spring Boot项目, 勾选的依赖和上面那个一样
具体依赖如下
<?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.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liy</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.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.properties
spring.application.name=consumer
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
server.port=8083
也是在启动类里添加个注解
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
RestTemplate rs(){
return new RestTemplate();
}
@Bean
//添加这个注解 ,即是开启负载均衡
@LoadBalanced
RestTemplate rs2(){
return new RestTemplate();
}
}
然后写个控制类去发现去调用provider服务项目里的接口
package com.liy;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
@RestController
public class HelloController {
@Autowired
DiscoveryClient ds;
@Autowired
@Qualifier("rs")
RestTemplate rs;
@Autowired
@Qualifier("rs2")
RestTemplate rs1;
int count = 0;
@GetMapping("/hello")
public String hello() throws Exception {
List<ServiceInstance> instances = ds.getInstances("provider");
ServiceInstance instance = instances.get(count++ % instances.size());
String s = "http://" + instance.getHost() + ":" + instance.getPort() + "/hello";
//String s1 = rs.getForObject(s, String.class);
//return s1;
URL url = new URL(s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
if (conn.getResponseCode()==200){
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String s1 = reader.readLine();
return s1;
}
return "";
}
@GetMapping("/hello2")
public String hello2(){
String object = rs1.getForObject("http://provider/hello", String.class);
return object;
}
}
三个方式去调用provider的接口 ,并且获取到数据
第一种
第二种就是上面的hello()方法 ,这个方法调用的是最原始的路径跳转还是什么的
第三种就是下面那个hello2() 方法
@GetMapping("/hello2")
public String hello2(){
String object = rs1.getForObject("http://provider/hello", String.class);
return object;
}
然后启动这个项目 , 调用其中一个方法 , 如果显示了provider服务项目里的接口返回的数据即是成功了 ,前面的注册中心和provider都要是启动状态