Spring Cloud微服务的服务治理组件eureka(三)
2021-04-22 06:28
                         标签:depend   cat   ipa   admin   ref   图片   main   pre   健康      在eureka服务治理体系中,主要分为服务端和客户端两个不同的角色,服务端为服务注册中心,客户端为各个提供接口的微服务应用。    客户端的配置主要分为两个方面:         开发工具:idea                                       并注入restTemple,用于rest请求;        出现以下日志代码启动成功                     >       我是 啧啧啧花儿不谢呀! ,一只有理想,有故事的程序员,欢迎说出你的故事   Spring Cloud微服务的服务治理组件eureka(三) 标签:depend   cat   ipa   admin   ref   图片   main   pre   健康    原文地址:https://www.cnblogs.com/flyPenguinblog/p/13278498.html目录
背景
eureka服务提供者搭建
服务整合
 
一、背景
二、eureka服务消费者搭建
  1、File---> New ---> Project---->spring Initialzr  

 
2、填写项目包名、项目名,继续NEXT


 
 3、选中Discovery Client和web模块、Next-->(选择项目目录)Finish

4、项目生成后目录结构如下,其中controller、service、api为新建的包,其他文件及目录是默认生成的

 5、我们来看看pom.xml
1 
6、启动类加上EnableDiscoveryClient注解,自动化配置为服务治理的客户端
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
    @Bean
  RestTemplate restTemplate(){
      return new RestTemplate();
  }}
7、application.properities配置
1 server.port=8089
2 spring.application.name=eureka-ribbon
#注册的服务中心的路径,这里注册了多个代表注册了一个服务中心集群
3 eureka.client.service-url.defaultZone=http://admin:123456@localhost:1111/eureka/,http://admin:123456@localhost:1112/eureka/
4 #IP进行注册
5 eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
6 eureka.instance.preferIpAddress=true 
8、Controller提供服务请求入口,处理参数。
 1 @RestController
 2 public class SysController {
 3 
 4     @Autowired
 5     SysService sysService;
 6 
 7     @ResponseBody
 8     @RequestMapping("/hi")
 9     public String greetService(@RequestParam("service_name")String service_name){
10         return sysService.hiService(service_name);
11     }
12 }
 
service用于编写业务代码:
eureka-client为服务实例
@Service
public class SysService {
    @Autowired
    RestTemplate restTemplate;
    public String hiService(String service_name){
        return restTemplate.getForObject("http://eureka-client/demo?name="+service_name,String.class);
    }
}
9、点击以下图标启动服务


10、访问注册中心localhost:1111,如图就是我们的实例

三、服务整合

文章标题:Spring Cloud微服务的服务治理组件eureka(三)
文章链接:http://soscw.com/essay/77954.html