springCloud 之 Eureka服务治理机制及代码运行
2021-05-17 09:28
标签:pre sel host 返回 beans The hide 告诉 请求转发 配置文件 启动 接口的类 结果 springCloud 之 Eureka服务治理机制及代码运行 标签:pre sel host 返回 beans The hide 告诉 请求转发 原文地址:https://www.cnblogs.com/xiaoyao-001/p/9736734.html 1 #定义注册中心端口号为8888
2 server:
3 port: 8888
4 spring:
5 application:
6 name: server-center1
7 eureka:
8 instance:
9 hostname: center1
10 client:
11 service-url:
12 defaultZone: http://127.0.0.1:9999/eureka/
13 # 注册中心没必要将自己注册给自己
14 register-with-eureka: false
15 # 注册中心不需要去检索服务,调用服务,故而不需要获取注册服务清单
16 fetch-registry: false
17 server:
18 peer-node-read-timeout-ms: 12000
19 enable-self-preservation: false
1 package com.zxy.forward;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6 @EnableEurekaServer
7 @SpringBootApplication
8 public class Center1Application {
9
10 public static void main(String[] args) {
11 SpringApplication.run(Center1Application.class, args);
12 }
13 }
1 server:
2 port: 9999
3 spring:
4 application:
5 name: server-center2
6 eureka:
7 instance:
8 appname: center2
9 client:
10 service-url:
11 defaultZone: http://127.0.0.1:8888/eureka/
12 # 注册中心没必要将自己注册给自己
13 register-with-eureka: false
14 # 注册中心不需要去检索服务,调用服务,故而不需要获取注册服务清单
15 fetch-registry: false
16 server:
17 # 设置读取超时时间
18 peer-node-read-timeout-ms: 12000
19 # 关闭自我保护
20 enable-self-preservation: false
1 package com.zxy.demo;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
7 import org.springframework.scheduling.annotation.EnableScheduling;
8
9 @SpringBootApplication
10 @EnableEurekaServer
11 public class Center2Application {
12
13 public static void main(String[] args) {
14 SpringApplication.run(Center2Application.class, args);
15 }
16 }
server:
port: 1001
spring:
application:
name: provider
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8888/eureka/
# 服务提供者如果也需要作为消费者则可以获取注册清单,如果单纯作为服务提供者则不需要获取服务清单
fetch-registry: false
# 服务提供者需要提供服务,所以需要将自己注册在注册中心上
register-with-eureka: false
# 服务提供者需要把下面的实例名关闭,否则在按照消费端的代码调用是会找不到主机名(启动单实例提供者把下面注释掉)
# instance:
# hostname: provider1
1 package com.zxy.demo;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6
7 @SpringBootApplication
8 @EnableDiscoveryClient
9 public class ProviderApplication {
10
11 public static void main(String[] args) {
12 SpringApplication.run(ProviderApplication.class, args);
13 }
14 }
1 package com.zxy.demo.proapi;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.web.bind.annotation.RequestMapping;
6 import org.springframework.web.bind.annotation.RestController;
7
8 @RestController
9 public class HelloWorldController {
10 private Logger logger = LoggerFactory.getLogger(this.getClass());
11
12 @RequestMapping(value="/welcome")
13 public String getWelcome() {
14 String str = "hello,welcome the world!!!";
15 logger.info(str);
16 System.out.println(str);
17 return str;
18 }
19
20 }
1 server:
2 port: 2001
3 spring:
4 application:
5 name: consumer
6 eureka:
7 # instance:
8 # hostname: consumer
9 client:
10 service-url:
11 defaultZone: http://127.0.0.1:8888/eureka
12 # 如果需要提供接口共别人调用也可以将其注册在注册中心
13 # register-with-eureka: false
14 #获取服务是服务消费者的基础,故而下面的设置必须为true,默认是true
15 # fetch-registry: false
1 package com.zxy.demo;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.web.client.RestTemplate;
9
10 @SpringBootApplication
11 @EnableDiscoveryClient
12 public class ConsumerApplication {
13
14 @Bean
15 @LoadBalanced
16 RestTemplate restTemplate() {
17 return new RestTemplate();
18 }
19 public static void main(String[] args) {
20 SpringApplication.run(ConsumerApplication.class, args);
21 }
22
23 }
1 package com.zxy.demo.consumer;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestMethod;
6 import org.springframework.web.bind.annotation.RestController;
7 import org.springframework.web.client.RestTemplate;
8
9 @RestController
10 public class ConsumerController {
11 @Autowired
12 private RestTemplate template;
13 @RequestMapping(value="/msg0",method=RequestMethod.GET)
14 public String getStr() {
15 System.out.println("I am consumer msg0!");
16 return template.getForEntity("http://provider/welcome", String.class).getBody();
17 }
18
19 @RequestMapping(value="/msg1",method=RequestMethod.GET)
20 public String msg() {
21 RestTemplate t = new RestTemplate();
22 System.out.println("I am consumer msg1!");
23 return t.getForEntity("http://127.0.0.1:1001/welcome", String.class).getBody();
24 }
25 }
下一篇:JAVA实现随机四则运算
文章标题:springCloud 之 Eureka服务治理机制及代码运行
文章链接:http://soscw.com/index.php/essay/86655.html