非常感谢https://www.cnblogs.com/sam-uncle/p/8954401.html的作者,借鉴其文章自己动手做了一遍,现将自己动手过程记录下来,方便后面回顾
工程
1)eureka 服务注册中心
2)hello-eureka-service 服务注册
3)hello-eureka-consumer 服务发现
idea创建spring boot项目
删除这三个文件/目录
一、服务注册中心
1)目录结构
2)pom文件
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 使用dependencyManagement进行版本管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入eureka server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
3)启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
application.yaml配置文件
server:
port: 8030
spring:
application:
name: eureka
eureka:
instance:
hostname: eureka-service
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
4)启动项目,浏览器输入localhost:8030,可以看到如下界面:
目前还没有注册服务,所以此处是空的
二、服务注册
1)目录结构
2)pom文件
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
3)application.yaml配置文件,eureka.client.service-url.defaultZone配置的地址为上方服务中心地址
server:
port: 8040
spring:
application:
name: hello-eureka-service
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8030/eureka
4)启动类
@SpringBootApplication
@EnableEurekaClient
public class HelloEurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloEurekaServiceApplication.class, args);
}
}
5)controller
@RestController
public class HelloController {
@Autowired
DiscoveryClient discoveryClient;
@RequestMapping("/hello")
public String hello() {
ServiceInstance instance = discoveryClient.getLocalServiceInstance();
//打印服务的服务id
return "hello,my service id :" + instance.getServiceId();
}
}
6)启动项目,浏览器打开localhost:8030,会发现此处已经有服务注册
三、服务发现
1)目录结构
2)pom文件
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 引入ribbon 依赖 ,使服务发现时支持负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
3)application.yaml配置文件
server:
port: 8050
spring:
application:
name: hello-eureka-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8030/eureka
4)启动类
@EnableDiscoveryClient
@SpringBootApplication
public class HelloEurekaConsumerApplication {
//@Bean 应用在方法上,用来将方法返回值设为为bean
@Bean
@LoadBalanced //@LoadBalanced实现负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(HelloEurekaConsumerApplication.class, args);
}
}
5)controller
@RestController
public class HolleConsumerController {
//这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hello-consumer")
public String helloConsumer() {
//调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port
Object result = restTemplate.getForObject("http://hello-eureka-service/hello", String.class);
System.out.println("-----------------返回结果为:" + result);
return "hello consumer finish !!!";
}
}
6)启动,浏览器打开地址localhost:8030,可以看到此处已经有两个服务
7)浏览器请求http://localhost:8050/hello-consumer
控制台打印:
-----------------返回结果为:hello,my service id :hello-eureka-service
浏览器显示:
四、服务发现时实现负载均衡
hello-eureka-consumer的pom文件里有配置ribbon,将hello-eureka-service启动两个,端口分别为8040、8060
1)hello-eureka-service需要增加一个获取当前应用端口的方法
@Component
public class PortDemo implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private int serverPort;
@Override
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
this.serverPort = event.getEmbeddedServletContainer().getPort();
}
public int getPort() {
return this.serverPort;
}
}
2)hello-eureka-service的HelloController需要将端口一起返回
@RestController
public class HelloController {
@Autowired
DiscoveryClient discoveryClient;
@Autowired
private PortDemo portDemo;
@RequestMapping("/hello")
public String hello() {
ServiceInstance instance = discoveryClient.getLocalServiceInstance();
//打印服务的服务id
return "hello,my service id:" + instance.getServiceId() + ",port:" + portDemo.getPort();
}
}
3)idea同一spring boot项目以多个端口启动多个应用:
去掉右上角的勾,支持多次启动
配置好之后
切换到
启动,此时就启动了一个8040端口的hello-eureka-service
切换到
修改application.yaml配置文件server.port=8040为8060
server:
port: 8040
spring:
application:
name: hello-eureka-service
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8030/eureka
启动,此时就启动了一个8060端口的hello-eureka-service
4)此时浏览器请求http://localhost:8050/hello-consumer六次
控制台打印:
-----------------返回结果为:hello,my service id:hello-eureka-service,port:8060
-----------------返回结果为:hello,my service id:hello-eureka-service,port:8040
-----------------返回结果为:hello,my service id:hello-eureka-service,port:8060
-----------------返回结果为:hello,my service id:hello-eureka-service,port:8040
-----------------返回结果为:hello,my service id:hello-eureka-service,port:8060
-----------------返回结果为:hello,my service id:hello-eureka-service,port:8040