Eureka实现微服务的调用
1、版本2、Eureka的注册中心 (1)、pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.ap
·
1、版本
2、Eureka的注册中心
(1)、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.crazyit.cloud</groupId>
<artifactId>first-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--版本控制 Dalston.SR3 ===》1.3.3.RELEASE -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--与SpringCloud配置相关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--Eureka的服务器(注册中心)-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
(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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.crazyit.cloud</groupId>
<artifactId>first-police</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--定义版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringCloud的配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--Eureka服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
(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 = "/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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.crazyit.cloud</groupId>
<artifactId>first-person</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--定义版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringCloud的配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--Eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--Ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
</project>
(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)、启动测试
更多推荐
已为社区贡献1条内容
所有评论(0)