1. 配置服务端
案例中角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是我们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。
1.1 引入pom文件
<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>
</dependency>
</dependencies>
1.2 配置文件(application.yml)
spring:
application:
name: spring-boot-producer
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/
1.3 启动类
启动类中添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
1.4 编写提供服务
/**
* @author pengjw
* @date 2019年03月19日 17:03
* @description 服务生产者提供方法
* @Version 1.0
*/
@RestController
public class ProducerController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
}
启动类,就可在注册中心发现服务,如下图就是注册成功。
2.配置客户端
2.1 引入pom文件
<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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
</dependencies>
2.2 配置文件
spring:
application:
name: spring-boot-customer
server:
port: 8003
eureka:
client:
service-url:
defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/
feign:
hystrix:
enabled: true
2.3 编写调用的服务的接口
/**
* @author pengjw
* @date 2019年03月19日 17:09
* @description feign生产者服务接口
* @Version 1.0
* @FeignClient name中属性值和生产服务名一致
*/
@FeignClient(name = "spring-boot-producer")
public interface HelloRemote {
/**
* @author pengjw
* @date 2019/3/19 17:11
* @param name
* @return java.lang.String
* 方法名要和生产者方法名一致
*/
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
2.4 启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
/**
* @author pengjw
* @date 2019/3/19 20:32
* @return org.springframework.boot.web.servlet.ServletRegistrationBean
* spring boot 版本大于2.0就需要配置这个,否则就会报404
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
2.5 功能调用
/**
* @author pengjw
* @date 2019年03月19日 17:12
* @description 消费者控制层
* @Version 1.0
* HelloRemote 是获取服务的接口
*/
@RestController
public class HelloController {
@Autowired
private HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name);
}
}
启动类,在注册中心如下图所示就表示成功。
至此配置已经完成,项目启动顺序,依次启动eureka—>procuder–>customer,eureka配置问题可看上一篇
3. 测试
先输入:http://localhost:8002/hello?name=neo 检查sproducer服务是否正常
返回:hello neo,this is first messge
说明producer正常启动,提供的服务也正常。
浏览器中输入:http://localhost:8003/hello/neo
返回:hello neo,this is first messge
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。