服务注册中心eureka-server已经搭好,并且SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION提供一个hello服务
编写一个eureka-client-consumer服务消费者,去消费该服务
一、新建module,选择对应的springcloud模块,pom.xml如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>lf.liyouyou</groupId> <artifactId>spring-cloud-netflix-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>lf.youyou</groupId> <artifactId>spring-cloud-eureka-client-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud-eureka-client-consumer</name> <description>Demo project for Spring Boot</description> <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-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
二、启动类添加注解
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SpringCloudEurekaClientConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaClientConsumerApplication.class, args); } }
三、编写调用hello服务代码
首先编写一个可以远程调用服务feign接口,name属性配置需要调用的服务名称
package lf.youyou.lf.com; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name="spring-cloud-netflix-eureka-client-application") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); }
Controller
package lf.youyou.lf.com; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired HelloRemote helloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return helloRemote.hello(name); } }
四、配置application.properties
spring.application.name=spring-cloud-netflix-eureka-client-consumer server.port=9090 eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
启动服务,
访问注册中心 http://localhost:8000/ 查看eureka面板
再访问http://localhost:9090/hello/lf
返回:hello lf,nice to meet you!