eureka 是一个服务注册的主键,业务的提供方、消费方可以注册到 EurekaServer ,然后服务的消费方可以通过服务的提供方的应用名进行资源消费(本节以 RestTemplate 进行远程调用,注意,使用服务名进行调用时,要在 RestTemplate 的 Bean 上加上 @LoadBalanced ,因为可能存在多个业务提供方使用相同的应用名,所以 RestTemplate 默认使用存在多个使用相同应用名的策略进行服务注册)
2. Eureka 的使用 2.1 编写相对应的 EurekaServer 代码 2.1.1 引入对应的 pom 依赖2.1.2 编写启动类springcloud com.lidantao 1.0-SNAPSHOT 4.0.0 cloud-eureka-server-70018 8 org.springframework.cloud spring-cloud-starter-netflix-eureka-serverorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuator
package com.lidantao; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaServerMain7001.class, args); } }2.1.3 yml 配置文件
server: port: 7001 eureka: instance: hostname: localhost # eureka 服务端的实例名称 instance-id: eureka-server-7001 prefer-ip-address: true client: register-with-eureka: false # false 表示不向注册中心注册自己 fetch-registry: false # false 表示自己端是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: # 设置与 Eureka server 交互的地址和注册服务都需要依赖这个地址 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/2.2 编写相对应的 业务提供方-8001 代码 2.2.1 引入相对应的 pom 依赖
2.2.2 对应的业务代码springcloud com.lidantao 1.0-SNAPSHOT 4.0.0 cloud-provider-8001org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.projectlombok lombokorg.springframework.cloud spring-cloud-starter-netflix-eureka-client
package com.lidantao.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Value("${server.port}") private String server_port; @RequestMapping("/provider/getTest") public String getTest(){ return "getTest" + server_port; } }2.2.3 编写启动类
package com.lidantao; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProviderMain8001 { public static void main(String[] args) { SpringApplication.run(ProviderMain8001.class, args); } }2.2.4 yml 配置文件
server: port: 8001 spring: application: name: provider eureka: instance: # eureka 的主机名字 instance-id: provider-8001 # 悬浮显示 ip prefer-ip-address: true client: # true 表示向注册中心注册自己 register-with-eureka: true # true 表示向 EurekaServer 拉取自己的注册信息 fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka2.3 另一个 业务提供方-8002 代码 2.3.1 引入相对应 pom 依赖
2.3.2 编写相对应业务代码springcloud com.lidantao 1.0-SNAPSHOT 4.0.0 cloud-provider-8001org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.projectlombok lombokorg.springframework.cloud spring-cloud-starter-netflix-eureka-client
package com.lidantao.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Value("${server.port}") private String server_port; @RequestMapping("/provider/getTest") public String getTest(){ return "getTest" + server_port; } }2.3.3 启动类
package com.lidantao; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProviderMain8002 { public static void main(String[] args) { SpringApplication.run(ProviderMain8002.class, args); } }2.3.4 yml 配置文件
server: port: 8002 spring: application: name: provider eureka: instance: # eureka 的主机名字 instance-id: provider-8002 # 悬浮显示 ip prefer-ip-address: true client: # true 表示向注册中心注册自己 register-with-eureka: true # true 表示向 EurekaServer 拉取自己的注册信息 fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka2.4 编写 业务消费方 代码 2.4.1 引入相对应 pom 依赖
2.4.2 编写相对应业务代码springcloud com.lidantao 1.0-SNAPSHOT 4.0.0 cloud-consumer-808 8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.projectlombok lombokorg.springframework.cloud spring-cloud-starter-netflix-eureka-client
RestTemplate
package com.lidantao.config; 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 MyConfiguration { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
Controller
package com.lidantao.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Autowired private RestTemplate restTemplate; //private String PROVIDER_URL = "http://127.0.0.1:8001/provider/getTest"; private String PROVIDER_URL = "http://provider/provider/getTest"; @RequestMapping("/consumer/getTest") public String getTest(){ return restTemplate.getForObject(PROVIDER_URL, String.class); } }2.4.3 启动类
package com.lidantao; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConsumerMain80 { public static void main(String[] args) { SpringApplication.run(ConsumerMain80.class, args); } }2.4.4 yml 配置文件
server: port: 80 spring: application: name: consumer eureka: client: # true 表示向注册中心注册自己 register-with-eureka: true # true 表示向 EurekaServer 拉取自己的注册信息 fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka instance: # eureka 的主机名字 instance-id: consumer-80 # 悬浮显示 ip prefer-ip-address: true3. 父 pom 文件
4. Eureka 注册页面 5. 服务方消费结果(负载均衡效果)4.0.0 com.lidantao springcloudpom 1.0-SNAPSHOT cloud-provider-8001 cloud-consumer-80 cloud-eureka-server-7001 UTF-8 8 8 4.12 1.2.17 1.16.18 5.1.47 1.1.1 1.3.2 org.springframework.boot spring-boot-dependencies2.2.2.RELEASE pom import org.springframework.cloud spring-cloud-dependenciesHoxton.SR1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies2.2.0.RELEASE pom import
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)