springcloud-1.使用Eureka -robbon构建自动服务发现注册集群和消费者
2018-03-01 17:45
1356 查看
1.使用Eureka组件创建服务中心Server_1@EnableEurekaServer //表示自己是一个server
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
2.使用Eureka组件创建客服端Clinet_P_1,Clinet_P_2,即P表示生产者,服务提供者。并且指定服务中心的地址Server_1。@SpringBootApplication
@EnableEurekaClient //表示自己是一个client
@RestController
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}
3.使用ribbon组件创建客户端Client_C_1,C表示customer,服务消费者,并且指定服务中心的地址Server_1。
创建主类
调用远程服务@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
}
配置文件
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon
参考: http://blog.csdn.net/forezp/article/details/69788938
http://blog.csdn.net/forezp/article/details/69696915
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
2.使用Eureka组件创建客服端Clinet_P_1,Clinet_P_2,即P表示生产者,服务提供者。并且指定服务中心的地址Server_1。@SpringBootApplication
@EnableEurekaClient //表示自己是一个client
@RestController
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}
3.使用ribbon组件创建客户端Client_C_1,C表示customer,服务消费者,并且指定服务中心的地址Server_1。
创建主类
@SpringBootApplication @EnableDiscoveryClient public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
调用远程服务@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
}
配置文件
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon
参考: http://blog.csdn.net/forezp/article/details/69788938
http://blog.csdn.net/forezp/article/details/69696915
相关文章推荐
- SpringCloud构建微服务入门架构(二)服务注册与发现Eureka集群搭建
- Spring cloud Eureka服务注册及发现(三)发现使用服务
- springCloud学习01之eureka服务发现-提供者-消费者ribbon/feign-负载均衡
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
- springcloud学习笔记一:eureka服务注册与发现
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
- springcloud(第三篇)springcloud eureka 服务注册与发现
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)
- 《Spring Cloud Netflix》-- 服务注册和服务发现-Eureka的服务认证和集群
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
- Spring Cloud 服务注册和服务发现-Eureka 的服务认证和集群
- 史上最简单的SpringCloud教程 | 第一篇: 服务的注册与发现(Eureka)
- 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)
- Spring Cloud构建微服务架构:Eureka服务注册与发现
- (二)SpringBoot+SpringCloud —— 使用Eureka实现服务注册与发现
- 服务注册和服务发现-Eureka的服务认证和集群--Spring Cloud Netflix
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
- 《Spring Cloud Netflix》-- 服务注册和服务发现-Eureka的服务认证和集群
- 使用Docker搭建consul集群+registrator实现服务自动注册。