1、版本
文章图片
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实现微服务的调用】
文章图片
推荐阅读
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- jar|springboot项目打成jar包和war包,并部署(快速打包部署)
- 数据库|效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- java人生|35K 入职华为Java开发那天,我哭了(这 5 个月做的一切都值了)
- Java毕业设计项目实战篇|Java项目:在线嘿嘿网盘系统设计和实现(java+Springboot+ssm+mysql+maven)
- 微服务|微服务系列:服务发现与注册-----Eureka(面试突击!你想了解的Eureka都在这里.持续更新中......)
- 每日一书|每日一书丨终于有人把云原生讲明白了
- java|ApplicationListener和SpringApplicationRunListener的联系
- Spring|SpringSecurity--自定义登录页面、注销登录配置
- 性能|性能工具之 Jmeter 通过 SpringBoot 工程启动