spring cloud --eureka --ribbon
服务容器:
项目添加Eureka Server
<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> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>
修改配置文件
spring: application: name: eureka-server server: port: 2001 eureka: server: enable-self-preservation: false #eureka 的自我保护状态 instance: hostname: eureka1 #集群通过hostname来区分 client: register-with-eureka: false #不向自身注册 fetch-registry: false #不从自身拉取注册信息
主程序入口修改
@EnableEurekaServer @SpringBootApplication public class SpEurekaApplication { public static void main(String[] args) { SpringApplication.run(SpEurekaApplication.class, args); } }
修改hosts文件,添加域名映射
127.0.0.1 eureka1 127.0.0.1 eureka2
服务提供:
1. 添加eureka依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2. 修改application.yml添加eureka注册配置
eureka: client: service-url: defaultZone: http://eureka1:2001/eureka #为容器中的server.port的端口
3. 为这个项目的主程序添加EnableDiscoveryClient注解
eureka的集群设置
在容器的application.yml中 ,使用不同的配置启动两个主程序
spring: profiles: active: eureka2 --- spring: profiles: eureka1 application: name: eureka-server server: port: 2001 eureka: instance: hostname: eureka1 client: register-with-eureka: true #profile的配置会覆盖公用配置 fetch-registry: true #profile的配置会覆盖公用配置 service-url: defaultZone: http://eureka2:2002/eureka #eureka1启动时向eureka2注册 --- spring: profiles: eureka2 application: name: eureka-server server: port: 2002 eureka: instance: hostname: eureka2 client: register-with-eureka: true #profile的配置会覆盖公用配置 fetch-registry: true #profile的配置会覆盖公用配置 service-url: defaultZone: http://eureka2:2002/eureka #eureka1启动时向eureka2注册
服务的提供者的application.yml文件修改
eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
消费者:ribbon
添加EurekaDiscoveryClient依赖
application.yml文件
spring: application: name: ribbon server: port: 3001 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
主程序修改
@EnableDiscoveryClient @SpringBootApplication public class SpRibbonApplication { //创建 RestTemplate 实例,并存入 spring 容器 //@LoadBalanced //负载均衡注解 @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SpRibbonApplication.class, args); } }
使用时如在controller中:
@RestController public class RibbonController { @Autowired private RestTemplate rt; @GetMapping("/item-service/{orderId}") public JsonResult<List<Item>> getItems(@PathVariable String orderId) { //这里服务器路径用 service-id 代替,ribbon 会向服务的多台集群服务器分发请求 //return rt.getForObject("http://localhost:8101/{1}", JsonResult.class, orderId); return rt.getForObject("http://item-service/{1}", JsonResult.class, orderId); } }
相关推荐
小鱿鱼 2020-06-22
qingjiuquan 2020-04-23
yupi0 2020-10-10
spring 2020-08-18
编程点滴 2020-07-29
幸运小侯子 2020-07-05
itjavashuai 2020-07-04
qingjiuquan 2020-06-29
shushan 2020-06-25
咻pur慢 2020-06-18
melonjj 2020-06-17
qingjiuquan 2020-06-13
neweastsun 2020-06-05
小鱿鱼 2020-06-05
mxcsdn 2020-05-31
吾日五省我身 2020-05-27
牧场SZShepherd 2020-05-27