=============================>Eureka中心配置
注册中心服务的配置:
POM依赖
YML配置文件:
server:
port: 5001
spring:
application:
name: eureka-register
eureka:
client:
serviceUrl:
defaultZone: http://com.lcg.test0:5001/eureka
registerWithEureka: false #实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
fetchRegistry: false #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
server:
enableSelfPreservation: false #关闭Eureka的自我保护
启动类示例代码:
package com.zbf.register;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
- 作者:LCG
- 创建时间:2019/3/16 21:45
- 描述:Eureka中心启动类示例代码
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaRegisterServer {
public static void main(String[] args) {
SpringApplication.run ( EurekaRegisterServer.class,args );
}
}
本人项目截图:
================================>服务提供配置
Eureka服务提供方配置
POM
YML的配置
server:
port: 1001 #eureka-server
spring:
application:
name: service-provider
eureka:
client:
instance: com.lcg.test1
serviceUrl:
defaultZone: http://com.lcg.test0:5001/eureka #注册中心的地址
启动类示例代码:
package com.zbf.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
-
作者:LCG
-
创建时间:2019/3/16 21:29
-
描述:
*/
@SpringBootApplication
@EnableEurekaClient
public class EurekaServerApplication {public static void main(String[] args) {
SpringApplication.run ( EurekaServerApplication.class,args );
}
}
服务提供的Controller的示例代码
package com.zbf.service.provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
-
作者:LCG
-
创建时间:2019/3/16 21:33
-
描述:Eureka的测试端
*/
@RestController
public class TestEurekaServerController {@RequestMapping(“test01”)
public String test1(HttpServletRequest request, @RequestParam(“string”) String string){
System.out.println ("=server="+string);
return “this is server return”;
}@RequestMapping(“test11”)
public String test11(HttpServletRequest request, @RequestParam(“string”) String string){
System.out.println ("=server=11"+string);
return “this is server return11”;
}
}
项目结构截图
====================================>服务消费方配置
Eureka服务方消费者配置(服务端feign使用,熔断器使用)
POM
YML配置:
server:
port: 1010
spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://com.lcg.test0:5001/eureka
feign:
hystrix:
enabled: true #开启熔断器失败回调
启动类示例:
package com.zbf.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
- 作者:LCG
- 创建时间:2019/3/16 23:49
- 描述:
*/
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients //feign调用远程服务
@EnableCircuitBreaker //使用熔断器
public class EurekaClient1Application {
public static void main(String[] args) {
SpringApplication.run ( EurekaClient1Application.class,args );
}
}
消费端的示例代码:
远程接口调用类:
package com.zbf.consumer.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
-
作者:LCG
-
创建时间:2019/3/16 23:55
-
描述:使用feign远程调用服务端
-
fallback 失败回调的接口实现类
*/
@FeignClient(name = “service-provider”,fallback = CallFailBack.class)
@Component
public interface TestClient {@RequestMapping("/test01")
public String test01(@RequestParam(“string”) String string);@RequestMapping("/test11")
public String test11(@RequestParam(“string”) String string);
}
接口调用失败的回调类示例:
package com.zbf.consumer.client;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
/**
-
作者:LCG
-
创建时间:2019/3/17 11:33
-
描述:
*/
@Component
public class CallFailBack implements TestClient {
@Override
public String test01(String string) {
System.out.println (“test01失败回调”);
return null;
}@Override
public String test11(String string) {
System.out.println (“test11的失败回调”);
return null;
}
}
消费者的访问接口示例:
package com.zbf.consumer.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
-
作者:LCG
-
创建时间:2019/3/16 23:54
-
描述:
*/
@RestController
@RequestMapping(“test1”)
public class TestEurekaClient {@Autowired
private TestClient testClient;@RequestMapping("/test02")
public String test01(){return testClient.test01 ( "123456中国万岁!" );
}
@RequestMapping("/test22")
public String test21(){
return testClient.test11 ( “123456中国万岁!” );
}
}
本人项目结构图: