博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud之Eureka入门使用
阅读量:5978 次
发布时间:2019-06-20

本文共 6662 字,大约阅读时间需要 22 分钟。

搭建Eureka服务端(注册中心)

 

新建一个maven项目 项目名为eureka-server

 

pom.xml

4.0.0
com.zns
eureka-server
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR4
pom
import
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-eureka-server

 

 

application.yml

server:  port: 8761spring:  application:    name: my-eureka-servereureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false

 

在启动类增加注解@EnableEurekaServer

package com.zns;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

 

 

启动项目 访问 

 

搭建Eureka客户端(服务提供者)

 

新建一个maven项目,项目名为eureka-client-provider

 

pom.xml

4.0.0
com.zns
eureka-client-provider
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR4
pom
import
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-eureka

 

 

application.yml

server:  port: 8001spring:  application:    name: my-providereureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/

 

在启动类增加注解@EnableEurekaClient

package com.zns;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

 

 

增加一个PersonController

package com.zns.controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.zns.model.Person;@RestController@RequestMapping("/person")public class PersonController {    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)    public Person get(@PathVariable Integer id) {        Person p = new Person();        p.setId(id);        p.setName("name" + id);        return p;    }}

 

 

启动项目,再次刷新浏览 可以看到提供者已经注册到了Eureka

 

搭建Eureka客户端(服务调用者) 

 

新建一个mave项目,项目取名为eureka-client-consumer

 

pom.xml

4.0.0
com.zns
eureka-client-consumer
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR4
pom
import
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-ribbon

 

 

这里引入了Ribbon为例来调用服务

 

application.yml

server:  port: 9001spring:  application:    name: my-consumereureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/

 

在启动类增加注解@EnableEurekaClient

@SpringBootApplication@EnableEurekaClientpublic class Application {    @Bean    @LoadBalanced    public RestTemplate getRestTemplate() {        return new RestTemplate();    }        public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    }}

 

 

增加一个TestController 来测试调用提供者

package com.zns.controller;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.client.RestTemplate;@Controller@Configurationpublic class TestController {    @Autowired        private RestTemplate restTemplate;    @GetMapping("/personGetTest")    @ResponseBody    public String personGetTest() {        String res= restTemplate.getForObject("http://my-provider/person/get/1", String.class);        return res;    }}

 

启动项目 访问 可以看到成功调用提供者服务

 

转载于:https://www.cnblogs.com/zengnansheng/p/10389833.html

你可能感兴趣的文章