spring|Eureka实现微服务的调用

1、版本 spring|Eureka实现微服务的调用
文章图片

2、Eureka的注册中心 (1)、pom.xml

4.0.0 org.crazyit.cloud first-server 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-dependencies Dalston.SR3 pom import org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka-server

(2)、编写启动类
package org.crazyit.cloud; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication /*开启Eureka的注册中心*/ @EnableEurekaServer public class ServerApp { public static void main(String[] args) { new SpringApplicationBuilder(ServerApp.class).web(true).run(args); }}

(3)、配置文件(1、不去注册中心注册自己2、不去注册中心抓取服务文件)
server: port: 8761 eureka: client: #不去注册中心注册自己(自己就是服务中心) register-with-eureka: false #不去注册中心抓取服务文件(自己就是服务中心) fetch-registry: false

(4)、启动测试
直接运行main


3、服务提供者 (1)、pom.xml

4.0.0 org.crazyit.cloud first-police 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-dependencies Dalston.SR3 pom import org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka

(2)、编写启动类

package org.crazyit.cloud; public class Police { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

package org.crazyit.cloud; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication /*开启Eureka客户端*/ @EnableEurekaClient public class PoliceServer { public static void main(String[] args) { new SpringApplicationBuilder(PoliceServer.class).web(true).run(args); }}

(3)、配置文件(1、注册的服务名称2、注册中心的地址)
spring: application: #Eureka服务名称 name: first-police eureka: client: serviceUrl: #Eureka的注册中心 defaultZone: http://localhost:8761/eureka/

(4)、编写服务
package org.crazyit.cloud; import org.springframework.http.MediaType; 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; /** * 服务提供者 */ @RestController public class PoliceController { @RequestMapping(value = "https://www.it610.com/call/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Police call(@PathVariable Integer id) { Police p = new Police(); p.setId(id); p.setName("angus"); return p; } }

(5)、启动测试
直接运行main

4、服务消费者 (1)、pom.xml
4.0.0 org.crazyit.cloud first-person 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-dependencies Dalston.SR3 pom import org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-ribbon

(2)、编写启动类
package org.crazyit.cloud; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication /*开启Eureka的服务端*/ @EnableEurekaClient public class PersonServer { public static void main(String[] args) { new SpringApplicationBuilder(PersonServer.class).web(true).run(args); }}

(3)、配置文件(1、注册的服务名称2、注册中心的地址)
package org.crazyit.cloud; 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 /*加载RestTemplate的配置*/ @Configuration public class TestController { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } @GetMapping("/router") @ResponseBody public String router() { RestTemplate tpl = getRestTemplate(); //到注册中心找服务并调用服务 String json = tpl.getForObject("http://first-police/call/1", String.class); return json; }}

(4)、编写服务
package org.crazyit.cloud; 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 /*加载RestTemplate的配置*/ @Configuration public class TestController { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } @GetMapping("/router") @ResponseBody public String router() { RestTemplate tpl = getRestTemplate(); //到注册中心找服务并调用服务 String json = tpl.getForObject("http://first-police/call/1", String.class); return json; }}

(5)、启动测试
http://localhost:8081/router
【spring|Eureka实现微服务的调用】spring|Eureka实现微服务的调用
文章图片

    推荐阅读