服务治理:Spring Cloud Euraka
- 第一步,搭建服务注册中心(如果用zk作为注册中心,类似安装配置启动zk)
注册中心,我们部署两套,避免单点(小集群) /** * @EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话 */ @SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); System.out.println("Spring Cloud Eureka Server1 start success!"); } } 注册中心1的application.yml核心配置: spring: application: name: eureka-server server: port: 8001 eureka: client: service-url: defaultZone: http://172.16.1.30:8001/eureka 定义注册中心的地址,服务注册需要制定 @SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); System.out.println("Spring Cloud Eureka Server2 start success!"); } } 注册中心2的application.yml核心配置: spring: application: name: eureka-server server: port: 8001 eureka: client: service-url: defaultZone: http://172.16.1.31:8001/eureka 定义注册中心的地址,服务注册需要制定
http://172.16.1.30:8001/eureka
http://172.16.1.31:8001/eureka
- 第二步,注册服务提供者
假设服务已经写好了,服务需要启动: @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableHystrix @EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true) public class ServiceApplication { public static void main(String[] args){ SpringApplication.run(ServiceApplication.class, args); System.out.println("Server start success"); } } application.yml关注配置: eureka: client: service-url: defaultZone: http://172.16.1.30:8001/eureka/,http://172.16.1.31:8001/eureka/ 指定了服务注册中心的地址(服务往哪里注册,指定的即为上面注册中心的两个地址)
- 第三步,服务发现
TODO