大家好,又见面了,我是你们的朋友全栈君。
1、概念:待补充
2、入门
2.1、启动eureka server
此处示例是maven-module搭建,第一段为maven项目的dependency,后面的为module-springcloud-server 的示例dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
</dependencies>
package org.demo.eureka.server;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main( String[] args ){
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
配置文件
spring.application.name=spring-cloud-server
server.port =1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false
eurake server启动
spring cloud eurake server启动的入口在类
org.springframework.cloud.netflix.eureka.server.EurekaServerInitializerConfiguration
代码如下
@Override
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
try {
//TODO: is this class even needed now?
eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
log.info("Started Eureka Server");
publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
EurekaServerInitializerConfiguration.this.running = true;
publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
}
catch (Exception ex) {
// Help!
log.error("Could not initialize Eureka servlet context", ex);
}
}
}).start();
}
启动完成:如下所示,eureka server启动http服务监听1111端口
···
2017-11-26 10:06:02.898 INFO 840 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-11-26 10:06:02.898 INFO 840 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application spring-cloud-server with eureka with status UP
2017-11-26 10:06:02.916 INFO 840 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Setting the eureka configuration..
2017-11-26 10:06:02.916 INFO 840 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Eureka data center value eureka.datacenter is not set, defaulting to default
2017-11-26 10:06:02.916 INFO 840 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Eureka environment value eureka.environment is not set, defaulting to test
2017-11-26 10:06:02.920 INFO 840 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2017-11-26 10:06:02.920 INFO 840 --- [ Thread-11] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2017-11-26 10:06:02.921 INFO 840 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2017-11-26 10:06:02.921 INFO 840 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2017-11-26 10:06:02.921 INFO 840 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2017-11-26 10:06:02.924 INFO 840 --- [ Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-11-26 10:06:02.966 INFO 840 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 1111 (http)
2017-11-26 10:06:02.966 INFO 840 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 1111
2017-11-26 10:06:02.968 INFO 840 --- [ main] org.demo.eureka.server.Application : Started Application in 3.856 seconds (JVM running for 4.325)
启动完成后,我们可以访问eureka server了
2.2、注册服务
上面的例子演示了如何启动eureka server,那么server启动了,如何将我们正在运行的业务系统当做服务注册到eureka server中供其他的业务系统调用呢,下面演示hello world之spring cloud eureka service provider
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
服务代码
package org.demo.eureka.provider.web;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index(HttpServletRequest request, HttpServletResponse response, @RequestParam String name) {
try {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
} catch (Exception e) {
e.printStackTrace();
}
return "hello world, you are " + name;
}
}
服务代码注册到eureka server
package org.demo.eureka.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main( String[] args ){
SpringApplication.run(Application.class, args);
}
}
配置文件
server.port=1001
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://192.168.54.240:1111/eureka/
再次访问eureka server,我们可以发现,服务已经注册到eureka server中了
2.3、服务调用
eureka service已经注册到服务中心了,那么已经注册的服务应该如何使用呢,我们仍然需要通过eureka server,明确都有那些服务可用
下面将演示hello world之eureka service consumer
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
springcloud eureka为c-s模式,分为client、server角色,其中client又可以细分为服务提供者、服务消费者。
服务提供者:
服务注册
服务续租
服务下线
服务消费者:
获取服务
服务调用
服务中心:
失效剔除
自我保护
服务状态同步
PeerAwareInstanceRegistryImpl
private void replicateInstanceActionsToPeers(Action action, String appName,
String id, InstanceInfo info, InstanceStatus newStatus,
PeerEurekaNode node) {
try {
InstanceInfo infoFromRegistry = null;
CurrentRequestVersion.set(Version.V2);
switch (action) {
case Cancel:
node.cancel(appName, id);
break;
case Heartbeat:
InstanceStatus overriddenStatus = overriddenInstanceStatusMap.get(id);
infoFromRegistry = getInstanceByAppAndId(appName, id, false);
node.heartbeat(appName, id, infoFromRegistry, overriddenStatus, false);
break;
case Register:
node.register(info);
break;
case StatusUpdate:
infoFromRegistry = getInstanceByAppAndId(appName, id, false);
node.statusUpdate(appName, id, newStatus, infoFromRegistry);
break;
case DeleteStatusOverride:
infoFromRegistry = getInstanceByAppAndId(appName, id, false);
node.deleteStatusOverride(appName, id, infoFromRegistry);
break;
}
} catch (Throwable t) {
logger.error("Cannot replicate information to {} for action {}", node.getServiceUrl(), action.name(), t);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/135136.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...