官术网_书友最值得收藏!

2.1 使用Eureka

接下來嘗試一個(gè)Eureka的示例。

新建一個(gè)Maven項(xiàng)目,在pom.xml中添加對Eureka服務(wù)端的依賴:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    新建EurekaServerApplication.java,添加@EnableEurekaServer注解:
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }

啟動(dòng)程序后,打開http://localhost:8080/就可以看到如圖2-2所示的監(jiān)控頁面,圖中展示了向Eureka注冊的所有客戶端。

圖2-2 服務(wù)注冊Eureka的監(jiān)控顯示結(jié)果

2.1.1 Eureka服務(wù)提供方

啟動(dòng)一個(gè)服務(wù)提供方并注冊到Eureka。新建一個(gè)項(xiàng)目并在pom.xml中添加依賴:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

新建EurekaProviderApplication.java,并對外暴露一個(gè)sayHello的HTTP接口。

    @SpringBootApplication
    @EnableEurekaClient
    @RestController
    public class EurekaProviderApplication {
        @RequestMapping("/sayHello")
        public  String sayHello(String name){
            return "hello "+name;
        }
        public static void main(String[] args) {
            new SpringApplicationBuilder(EurekaProviderApplication.class).web(true).
                run(args);
        }
    }

在application.yml配置Eureka服務(wù)端地址以及自身服務(wù)名稱:

    eureka:
        client:
            serviceUrl:
                defaultZone: http://localhost:8761/eureka/ #在注冊中心配置Eureka地址
    spring:
        application:
            name: myprovider #自身名字

啟動(dòng)EurekaProviderApplication后,再去看Eureka的監(jiān)控頁面,應(yīng)該能看到如圖2-3所示信息,表明服務(wù)已經(jīng)注冊到Eureka。

圖2-3 在Eureka上注冊成功

注意

還有一種方法就是直接使用原生的com.netflix.discovery.EurekaClient(對應(yīng)Spring Cloud的DiscoveryClient)。通常,盡量不要直接使用原始的Netflix的Eureka Client,因?yàn)镾pring已經(jīng)對其進(jìn)行封裝抽象,應(yīng)盡可能使用DiscoveryClient。

2.1.2 Eureka服務(wù)調(diào)用方

一旦在應(yīng)用中使用了@EnableDiscoveryClient或者@EnableEurekaClient,就可以從Eureka Server中使用服務(wù)發(fā)現(xiàn)功能。

利用如下代碼,通過DiscoveryClient可以從Eureka服務(wù)端獲得所有提供myprovider服務(wù)的實(shí)例列表,并根據(jù)獲得的ServiceInstance對象獲取每個(gè)提供方的相關(guān)信息,如端口IP等,從中選取第一個(gè)提供方,通過Spring的RestTemplate發(fā)起HTTP請求進(jìn)行調(diào)用。對于獲取到的提供方集合,我們根據(jù)什么規(guī)則去選取哪個(gè)提供方?能否做到負(fù)載均衡呢?這將在第5章詳細(xì)介紹。

    @SpringBootApplication
    @EnableDiscoveryClient
    @Slf4j
    @RestController
    public class EurekaClientApplication {
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
        @Autowired
        private DiscoveryClient client;
        @Autowired
        private RestTemplate restTemplate;
        @RequestMapping(value = "/sayHello" , method = RequestMethod.GET)
        public String sayHello(String name) {
            List<ServiceInstance> instances = client.getInstances("myprovider");
            if (! instances.isEmpty()) {
                ServiceInstance instance = instances.get(0);
                log.info(instance.getUri().toString());
                String result=restTemplate.getForObject(instance.getUri().toString()+
                    "/sayHello? name="+name, String.class);
                return  result;
            }
            return  "failed";
        }
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    }

此時(shí),我們通過HTTP請求調(diào)用/sayHello接口,則可以看到服務(wù)調(diào)用方從Eureka獲取服務(wù)提供方信息,并進(jìn)行調(diào)用的日志信息了。

提示

不要在@PostConstruct方法以及@Scheduled中(或者任何ApplicationContext還沒初始完成的地方)使用EurekaClient。其需要等待SmartLifecycle(phase=0)初始化完成才可以。

主站蜘蛛池模板: 文山县| 甘肃省| 威海市| 海盐县| 分宜县| 鄱阳县| 孝义市| 精河县| 永州市| 修水县| 平罗县| 会东县| 马鞍山市| 塘沽区| 丹寨县| 莱西市| 孝义市| 枝江市| 金塔县| 隆回县| 吐鲁番市| 扶风县| 汝城县| 周至县| 杭锦后旗| 高州市| 潼南县| 大埔区| 定襄县| 利川市| 鲁甸县| 环江| 嫩江县| 泰和县| 肥东县| 湖北省| 本溪市| 淳安县| 林口县| 武安市| 澜沧|