eureka 服务已经启动 http://localhost:8761/
注: 在启动文件加上 注解
@EnableEurekaServer
application.yml 配置文件
eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 相互注册实现高可用 register-with-eureka: false # true 将自己注册到eureka fetch-registry: false server: enable-self-preservation: false spring: application: name: eureka # 服务名称,注册之后的名称 server: port: 8761
clienta 服务注册eureka
启动文件加入注解:
@EnableEurekaClient
application.yml 配置文件 端口8000
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ #,http://localhost:8762/eureka/,http://localhost:8763/eureka/ # instance: # hostname: clientName spring: application: name: clienta
@RestController public class TestController { @GetMapping("/msg") public String msg() { return "this is clienta msg!!!"; } }
访问 http://localhost:8000/msg 能返回结果
client 服务注册eureka 同clienta
在clien中调用clienta中方法
第一种方法:
@RestController public class HelloController { @Autowired LoadBalancerClient loadBalancerClient; @GetMapping("/test") public String test() { // 获取服务实例 ServiceInstance serviceInstance = loadBalancerClient.choose("CLIENTA"); // 根据实例获取host port String host = serviceInstance.getHost(); int port = serviceInstance.getPort(); // 拼接url String url = String.format("http://%s:%s%s", host, port, "/msg"); RestTemplate restTemplate = new RestTemplate(); String res = restTemplate.getForObject(url, String.class); return res; } }
第二种: 配置config
@Component public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
@RestController public class HelloController { @Autowired private RestTemplate restTemplate; @GetMapping("/test") public String test() { String res = restTemplate.getForObject("http://CLIENTA/msg", String.class); return res; } }
启动client 端口 8081
访问: http://localhost:8081/test
以上为基于RestTemplate 通信方式.
使用Feign通信
eureka clienta 项目不变化
修改 client 项目:
1. 添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2. 启动类添加注解
@EnableFeignClients
3. 创建一个接口 并增加注解
@FeignClient(name = "CLIENTA") // name是服务的名字 public interface ClientAClient { @GetMapping("/msg") // 基于路由匹配, 方法名可以不同 String msg(); }
4. 在controller中直接使用
@RestController public class HelloController { @Autowired private ClientAClient clientAClient; @GetMapping("/test") public String test() { return clientAClient.msg(); } }
附pom.xml文件 注意版本对应 http://spring.io/projects/spring-cloud 官网
<?xml version="1.0" encoding="UTF-8"?> <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>com.example</groupId> <artifactId>client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-commons</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>