一:服务注册中心 (Eureka Server)
-
创建maven工程
(1).新建eureka 服务,new module,选择
(2)下一步(注意路径),在下一步选择eureka server
-
完成创建,添加pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--可以不需要-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 添加application.properties配置信息
server.port= 9000
#服务注册中心实例的主机名
eureka.instance.hostname=localhost
#是否向服务注册中心注册自己
eureka.client.registerWithEureka= false
#是否检索服务
eureka.client.fetchRegistry= false
eureka.client.serviceUrl.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/
# 或者 eureka.client.serviceUrl.defaultZone= http://localhost:9000/eureka/
- 启动类`
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
- 编译启动即可,浏览器访问 127.0.0.1:9000看到Eureka的控制台(表示祖册成功)
二:服务提供者
- 创建服务,方式同上
- pom,同上
- 配置文件:application.properties
spring.application.name= spring-cloud-producer
server.port= 9100
eureka.client.serviceUrl.defaultZone= http://localhost:9000/eureka
- 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
- 创建TestController
@RestController
public class TestController {
@RequestMapping("/test")
public String test(String name) {
return "你好," + name;
}
}
- 启动服务,请求 http://localhost:9100/test?name=hello,spring_cloud
页面正常返回,说明我们的服务提供者配置正确
三:服务消费者
- 创建服务,方式同上
- pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 配置文件:application.properties
spring.application.name=spring-cloud-consumer
server.port=9200
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka
- 启动类
@SpringCloudApplication
@EnableFeignClients
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
- service接口
//value 值需要与服务提供者的 spring.application.name相同
@FeignClient(value = "spring-cloud-producer")
public interface ConsumerService {
//value值需要与服务通过需调用接口路径相同
@RequestMapping(value = "/test", method = RequestMethod.GET)
String test(@RequestParam("name") String name);
}
- collection
@RestController
public class ConsumerController {
@Autowired
ConsumerService consumerService;
@RequestMapping("/hello/{name}")
public String sayHello(@PathVariable("name") String name) {
return consumerService.test(name);
}
}
依次启动eureka、producer、consumer三个项目
浏览器中输入:http://127/0.0.1:9200/hello/你好我好大家好
返回:你好,你好我好大家好
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。