当前位置: 首页 > news >正文

关于网站建设投稿免费优化

关于网站建设投稿,免费优化,贵阳疫情最新情况最新消息今天,嘉兴做网站设计下面写一个简单的demo验证下eureka&#xff0c;实现服务注册、服务发现。 一、单节点&#xff1a; 1、api&#xff1a; 封装其他组件需要共用的dto 2、eureka-service服务注册中心&#xff1a; &#xff08;1&#xff09;pom: <?xml version"1.0" encoding&q…

 下面写一个简单的demo验证下eureka,实现服务注册、服务发现。

一、单节点:

1、api:

封装其他组件需要共用的dto

2、eureka-service服务注册中心:

(1)pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.cloud</groupId><artifactId>myspringcloud-eureka-server</artifactId><version>1.0-SNAPSHOT</version><!-- springBoot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Edgware.RELEASE</spring-cloud.version></properties><dependencies><!--eureka-server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

 (2)application.properties:在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以需要禁用它的客户端注册行为

server.port=1111
eureka.client.registerWithEureka=false  
eureka.client.fetchRegistry=false  
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

(3)启动类:

package com.demo.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class MyEurekaServerApplication {public static void main(String args[]){SpringApplication.run(MyEurekaServerApplication.class,args);}
}

启动后,访问http://localhost:1111/:

就可以从后台监控服务了,(是不是比dubbo搭建zk注册中心方便多了) ,此时还没有服务注册过来,可以看到application下是空的。

3、eureka-client注册服务提供者:将原有的springboot工程改造,注册到eureka注册中心

(1)pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.cloud</groupId><artifactId>myspringcloud-eureka-client</artifactId><version>1.0-SNAPSHOT</version><!-- springBoot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Edgware.RELEASE</spring-cloud.version></properties><dependencies><!--eureka-server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>com.demo.cloud.api</groupId><artifactId>myspringcloud-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mybatis --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.1</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.5</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

(2)application.properties:

server.port=2222
server.context-path=/myService
spring.application.name=my-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/#mybatis
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy
mybatis.mapper-locations=classpath*:Mapper/*Mapper.xml

(3)dao、service、Mapper:mybatis持久化部分,此处省略

(4)controller:

package com.demo.cloud.controller;import com.demo.cloud.dto.AuthorityDTO;
import com.demo.cloud.dto.UserDTO;
import com.demo.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RequestMapping("/user")
@RestController
public class UserApiController {@Autowiredprivate UserService userService;@Value("${server.port}")private String port;@RequestMapping("/findByUserName")public UserDTO findByUserName(String username){return userService.findByUserName(username);}@RequestMapping("/getAllUsers")public List<UserDTO> getAllUsers(){System.out.println(port);return userService.getAllUsers();}@RequestMapping("/getAuthortiesByUserId")public List<AuthorityDTO> getAuthortiesByUserId(Integer userId){return userService.getAuthortiesByUserId(userId);}@RequestMapping("/addUser")public void addUser1(@RequestBody UserDTO userDTO){userService.addUser(userDTO);}}

(5)启动类:

package com.demo.cloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.demo.cloud.dao")
public class MyEurekaClientApplication {public static void main(String args[]){SpringApplication.run(MyEurekaClientApplication.class,args);}
}

注意:这里使用的是@EnableEurekaClient注解,也可以使用@EnableDiscoveryClient注解, 

@EnableEurekaClient是Eureka特有的注解,用于启动Eureka客户端。当使用Eureka作为注册中心时,就推荐使用@EnableEurekaClient注解,应用启动后会自动注册到Eureka Server,并完成服务治理。
@EnableDiscoveryClient是Spring Cloud通用的注解,可以与Eureka、Consul等多种注册中心对接。当我们的微服务同时需要与多个注册中心集成时,就需要使用@EnableDiscoveryClient注解。
可以说,@EnableEurekaClient是@EnableDiscoveryClient的一个具体实现,如果项目中注册中心只使用Eureka,那么使用@EnableEurekaClient更加方便和简单。但如果要切换到其他的注册中心,改动较大。

启动项目,再浏览下http://localhost:1111/:

可以看到注册成功了。

二、高可用部署:

1、先部署eukera-service的高可用

2、再部署eureka-client的高可用:连接注册中心的配置项改为多个,以逗号分隔,然后将该组件部署多个节点即可

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://xxx.xx.xxx:1111/eureka/

http://www.hkea.cn/news/781543/

相关文章:

  • 建设学院网站百度收录关键词查询
  • 有关外贸的网站有哪些内容武汉抖音seo搜索
  • 娄底网站建设的话术深圳网站关键词排名优化
  • 福田大型商城网站建设seo营销方法
  • 网站开发专业就业指导企业网站设计与实现论文
  • 网络营销方式的思维导图seo关键词优化系统
  • wordpress访客ip记录福清市百度seo
  • 网站下载速度慢互联网广告推广公司
  • 电影网站空间配置网络营销的工具和方法有哪些
  • 包装设计网站免费百度seo搜索引擎优化厂家
  • 免费做公司网站sem对seo的影响有哪些
  • 网站空间购买费用关键词优化计划
  • 网站制作可以卖多少钱陕西网站建设制作
  • 深圳中小企业网站制作谷歌海外广告投放
  • 做游戏网站的需求分析创建app平台
  • 青岛胶南做网站的有多少seo商学院
  • 二月网站建设南宁百度个人中心登录
  • 如何在相关网站免费做宣传广告免费建立个人网站官网
  • 做搜狗网站优化首自己建立网站步骤
  • 企业资质查询官方网站最好的小说网站排名
  • 乐平网站设计北京互联网公司
  • 朝阳企业网站建设方案费用郑州网络营销学校
  • 建站行业发展百度广告代运营
  • 如何做积分商城网站鸡西seo顾问
  • p2p网站开发文档免费b站软件下载
  • 有没有做q版头像的网站今天百度数据
  • wordpress页面修改插件seo顾问阿亮
  • 政府门户网站建设标准国际婚恋网站排名
  • 上海青浦网站建设郑州靠谱seo电话
  • 网站建设怎么样seo专家招聘